繁体   English   中英

错误TS2345:“对象”类型的参数无法分配给类型的参数

[英]Error TS2345: Argument of type 'Object' is not assignable to parameter of type

我正在使用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 });

我该如何解决这个错误?

谢谢!

服务方法this.service.queryForCategory(this.category, { skip, take }); 期望第一个参数的类型为{ CategoryID: number; } { 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM