【发布时间】:2019-09-30 13:35:29
【问题描述】:
我的这段代码在 Angular 5 中运行良好,但我正在尝试更新到 Angular 8:
this.awsService.getProfiles().subscribe(profiles => {
this.profiles = profiles;
if (this.profiles.length > 0 && this.profiles.indexOf(this.currentProfile) == -1) {
this.currentProfile = this.profiles[0];
localStorage.setItem('profile', this.currentProfile);
}
}, err => {
this.profiles = [];
})
我收到了这个错误:
ERROR in app/app.component.ts:85:9 - error TS2696: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Type 'Object' is missing the following properties from type 'string[]': length, pop, push, concat, and 26 more.
85 this.profiles = profiles;
Angular 8 中的正确语法是什么?
【问题讨论】:
-
getProfiles()的返回类型是什么。 -
不要使用
Object来断言类型(参见docs)。 -
你确定代码在抱怨这段代码吗?该消息说您将类型 Object 分配给了某个属性,这是非常通用的,您必须在这种情况下使用 any。
标签: angular