错误TS2345:类型'对象'的参数不能分配给

时间:2016-11-26 20:15:34

标签: angular

我正在使用角度2,我有一个奇怪的错误:

error TS2345: Argument of type 'Object' is not assignable to parameter of type

我命令行显示:

[0] app/category-details.component.ts(40,39): error TS2345: Argument of type 'Object' is not assignable to parameter of type '{ CategoryID: number; }'.
[0]   Property 'CategoryID' is missing in type 'Object'.
[0] app/category-details.component.ts(45,39): error TS2345: Argument of type 'Object' is not assignable to parameter of type '{ CategoryID: number; }'.

这是代码:

export class CategoryDetailComponent implements OnInit {

    @Input() public category: Object;

    private view: Observable<GridDataResult>;
    private skip: number = 0;

    constructor(private service: ProductsService) { }

    public ngOnInit(): void {
        this.view = this.service;

        this.service.queryForCategory(this.category, { skip: this.skip, take: 5 });
    }

    protected pageChange({ skip, take }: PageChangeEvent): void {
        this.skip = skip;
        this.service.queryForCategory(this.category, { skip, take });
    }
}

它在行显示了这个错误:

this.service.queryForCategory(this.category, { skip: this.skip, take: 5 });

this.service.queryForCategory(this.category, { skip, take });

如何解决此错误?

谢谢!

1 个答案:

答案 0 :(得分:0)

服务方法this.service.queryForCategory(this.category, { skip, take });期望第一个参数的类型为{ CategoryID: number; },而您传递的this.category注释为Object。 您可以为类别创建界面,以便queryForCategory@Input() public category: ICategory;都使用它。

// interface.ts
export interface ICategory {
    CategoryID: number;
}

// service declaration
queryForCategory(category: ICategory, options)


// input property annotation
 @Input() public category: ICategory;