简体   繁体   English

构建时类型对象上不存在 Angular 4 属性

[英]Angular 4 Property does not exist on type Object on build

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:我正在使用 Angular 构建一个项目,我使用 angular-cli 启动了该项目,当我尝试运行ng build --prod我不断收到此错误:

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>

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?我需要做些什么来避免这种情况?

For your first example.对于你的第一个例子。 In your html, you are saying product has the property description (which it does not on type Object)在您的 html 中,您是说产品具有属性描述(它没有在对象类型上)

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;

Also as others have noted, you do not need the Object<> syntax另外正如其他人所指出的,您不需要 Object<> 语法

From my case..从我的情况..

ngOnInit(){
    this.product = this.request.getProduct(_id); // who is _id
} 

Just adding data:any at subscription works fine.只需添加数据:订阅中的任何内容都可以正常工作。

this.request.getProduct(_id).subscribe((data: any) => {
   this.product=data;
});

This would helpful while the response data having more key, value pairs.当响应数据具有更多键值对时,这将很有帮助。 (So Its hard/Time consuming to create Interface.) (因此创建界面很困难/耗时。)

But always the best practise is to use Interfaces ;)但最好的做法总是使用接口;)

First of all I would simply use product: ProductInterface;首先,我会简单地使用product: ProductInterface; and you don't even have to initialize it.你甚至不必初始化它。

Then, probably this will fix your error {{ product?. description }}然后,这可能会解决您的错误{{ product?. description }} {{ product?. description }}

If the property is dynamic (not known at compile time), you can use如果该属性是动态的(在编译时未知),则可以使用

someObject['someProperty']

instead of而不是

someObject.someProperty

You must define the request in OnInit method, your controller that implements OnInit interface and define a new method您必须在 OnInit 方法中定义请求,您的控制器实现 OnInit 接口并定义一个新方法

ngOnInit(){
    this.product = this.request.getProduct(_id); // who is _id
} 

Assuming that getProduct() is http request that return a observable假设 getProduct() 是返回 observable 的 http 请求

this.request.getProduct(_id).subscribe((data) => {
   this.product=data;
});

In my case it worked after set my properties as public在我的情况下,它在将我的属性设置为public 后工作

So, just change this所以,只要改变这个

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

to this对此

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

It is safe to use any rather than Object when data is requested from server, because we don't know what will be returned from server.当从服务器请求数据时,使用any而不是Object是安全的,因为我们不知道将从服务器返回什么。 So you don't need to typecheck ie:所以,你不需要类型检查,即:

export class AppComponent {
    product: any;

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

If you use command ng serve and build success.如果您使用命令ng serve并构建成功。

May,you have the trouble with build configuration可能,你在构建配置方面遇到了麻烦

maybe you need to modify the className.prod.ts like className.ts也许你需要修改className.prod.tsclassName.ts

my another answer has more detail:我的另一个答案有更多细节:

Angular - How to fix 'property does not exist on type' error? Angular - 如何修复“类型不存在属性”错误?

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

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