Angular 4属性在构建中的对象类型上不存在 [英] Angular 4 Property does not exist on type Object on build

查看:169
本文介绍了Angular 4属性在构建中的对象类型上不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular构建项目,我使用angular-cli启动了该项目,当我尝试运行ng build --prod时,我一直收到此错误:

I'm building a project using Angular, I started the project using angular-cli and when I try to run ng build --prod i keep getting this error:

对象类型上不存在属性说明"

Property 'description' does not exist on type Object

产生此错误的代码如下:

The code generating this error is the following:

export class AppComponent {
    product: Object = {};

    constructor(
        private store: StoreService,
        private request: RequestService,
    ) {
        this.product = this.request.getProduct(_id);
    }
}

<p>{{product.description}}</p>

我正在阅读一些有关此内容,错误是因为我正在使用类型定义将产品定义为Object,但没有传递任何属性定义.

I was reading some content about this and the error is because I'm using type definition to define product as Object, but I'm not passing any property definition.

我知道我可以像定义数组一样定义一个接口,但是我做不到.我不知道是否定义错误,这就是我尝试过的方法:

I know I could define an Interface, like I do with arrays, but I wasn't able to do it. I don't know if I'm defining it wrong, this is how I tried:

export interface ProductInterface {
    id: Number;
    description: String;
    title: String;
}

product: Object<ProductInterface> = {};

但这也给我错误.我需要怎么做才能避免这种情况?

But it also gives me errors. What do I need to do to avoid this?

推荐答案

第一个示例.在您的html中,您说的是产品具有属性描述(它不在Object类型上)

For your first example. In your html, you are saying product has the property description (which it does not on type Object)

在第二个示例中.您最初将产品定义为空对象

In your second example. You are initially defining product as an empty object

product: ProductInterface = {};

缺少接口的必填字段.因此,您可以删除初始化,而保留

Which is missing the required fields of the interface. So you can remove the initialization, leaving

product: ProductInterface;

正如其他人所指出的那样,您不需要Object<>语法

Also as others have noted, you do not need the Object<> syntax

这篇关于Angular 4属性在构建中的对象类型上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆