angular - 类型对象上不存在属性
问题描述
代码:
import { Component, OnInit } from '@angular/core';
import {DataService} from '../data.service';
import {Observable} from 'rxjs';
import {ActivatedRoute} from '@angular/router';
@Component({
selector: 'app-details',
templateUrl: './details.component.html',
styleUrls: ['./details.component.scss']
})
export class DetailsComponent implements OnInit {
user$: Object;
constructor(private data: DataService, private route: ActivatedRoute) {
this.route.params.subscribe(
params => this.user$ = params.id
);
}
ngOnInit() {
this.data.getUser(this.user$).subscribe(
(data: Object) => this.user$ = data
);
}
}
问题是当我尝试构建时它给出了这个错误:ng build --prod
Property 'name' does not exist on type 'Object'
我也尝试Object
用any
. 它没有成功。我是 Angular 6 的新手。
解决方案
可能您name
在模板中使用user$
变量绑定,因为 name$ 是您与异步数据的绑定。
因此,在这种情况下,最好的方法是——
user$: any;
并在模板中尝试像这样使用安全导航运算符(?
) -
{{name$?.name}}
推荐阅读
- r - 如何从 R 代码中的图表中删除十进制字母?
- angular - 数据绑定慢 - Angular 5
- symfony - 如何使用 symfony la 在 http://example.com?name=john&message=hello 之类的 url 上发出 get 请求来发送短信
- applet - 如何为 cinnamon Linux Mint 17.1 下载的应用程序创建小程序
- angular - 通过Angular 6中的指令为组件提供默认属性
- python - 拆分混合数据框的列
- javascript - 可见视频只需要像 Facebook 一样在网站上播放
- apache-spark - 需要调整长时间运行的火花作业
- python-3.x - python tkinter 尝试简化标签的东西
- ios - Swift 或 Objective-C 中 MDWAMP 的替代方案