【问题标题】:filter with undeclared type of array in Angular 2 Component在 Angular 2 组件中使用未声明类型的数组进行过滤
【发布时间】:2019-10-28 01:56:23
【问题描述】:

我的组件有一个过滤未声明类型数组的功能。

export class HomeComponent implements OnInit {
    public TaskList: []=[];
    public TaskTypes: any;

    constructor(){}

    ngOnInit() {

    this.TaskList=getTaskList();

          this.TaskTypes = [
            { test: "All Task", value: "all", class: "bg-primary", id: "all-task", icon: "fa-tasks", TaskList: this.TaskList },
            { test: "Ongoing", value: "ongoing", class: "bg-warning", id: "ongoing-task", icon: "fa-plug", TaskList: this.TaskList.filter(o => o.Status.includes("A")) },
            { test: "Hold", value: "hold", class: "bg-danger", id: "hold-task", icon: "fa-hand-paper", TaskList: this.TaskList.filter(o => o.Status.includes("H")) },
            { test: "Completed", value: "completed", class: "bg-success", id: "completed-task", icon: "fa-calendar-check", TaskList: this.TaskList.filter(o => o.Status.includes("C")) }
          ];
      }
    }

我无法为 TaskList 创建 class.ts 文件,因为 TaskList 的属性不是固定的,但属性“状态”是固定的。

一切正常,但编译时出现错误。

src/app/home/home.component.ts(85,142) 中的错误:错误 TS2339: “从不”类型不存在属性“状态”。 src/app/home/home.component.ts(86,138):错误 TS2339:属性 “从不”类型不存在“状态”。 src/app/home/home.component.ts(87,158):错误 TS2339:属性 “从不”类型上不存在“状态”。

这种过滤器的最佳方法是什么?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    只需将“any”添加到数组类型 公共任务列表:任何[] = [];

    【讨论】:

      【解决方案2】:

      这里:

      public TaskList: []=[];
      

      这就是 TS 的“type 'never'”错误的来源。将其声明为anys 的数组:

      public TaskList: any[] = [];
      

      这里是关于“从不”类型的文档:

      https://www.typescriptlang.org/docs/handbook/basic-types.html

      【讨论】:

        猜你喜欢
        • 2016-11-18
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 1970-01-01
        • 2016-08-28
        • 2016-11-02
        • 2017-01-17
        • 2015-02-20
        相关资源
        最近更新 更多