gpt4 book ai didi

angular - 我该如何解决此错误后续属性声明必须具有相同的类型和重复的标识符。 Angular 9

转载 作者:行者123 更新时间:2023-12-02 10:45:00 25 4
gpt4 key购买 nike

* 我真的不知道也不明白为什么会这样,我是 Angular 新手 *

  • 下面我将给出我的完整代码和完整的错误

错误后续的属性声明必须具有相同的类型。属性“post1”必须是“Posts”类型,但这里有类型“any”Duplicate identifier 'post1'

课后

export class Posts{
Key:string;
Email:string;
Password:string;
}

我在其中使用 post class

的 AppComponent 类
export class AppComponent {
title = 'proyecto-nro2';

post1 = new Posts();
//Said the error above in every post1.
post1.Key = 'NONE';
post1.Email = 'NONE';
post1.Password = '2';
}

最佳答案

属性不能直接在 ES6 中的类上初始化。在构造函数中初始化 post 对象并设置属性。

export class AppComponent {
title = 'proyecto-nro2';

post1: Posts;

constructor(){
this.post1 = new Posts();
this.post1.Key = 'NONE';
this.post1.Email = 'NONE';
this.post1.Password = '2';

}
}

其他选项:

class Posts {
Key: string;
Email: string;
Password: string;

constructor(key: string, email: string, password: string) {
this.Key = key;
this.Email = email;
this.Password = password;
}
}

export class AppComponent {
title = 'proyecto-nro2';
post1: Posts = new Posts('NONE', 'NONE', '2');
}

关于angular - 我该如何解决此错误后续属性声明必须具有相同的类型和重复的标识符。 Angular 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60218945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com