Angular 无法以角度指定数据源值
构造函数(私有appService:appService){} 新英雄:putDetail 数据源:MatTableDataSource; 恩戈尼尼特(){ this.appService.getComplaints() .subscribe(data=>this.newHero=data); this.dataSource=this.newHero; } 错误: 类型“putDetail”不可分配给类型 “MatTableDataSource”。类型中缺少属性“\u data” “细节”。(属性)ComplaintsComponent.dataSource: MatTableDataSourceAngular 无法以角度指定数据源值,angular,material-design,angular-material,Angular,Material Design,Angular Material,构造函数(私有appService:appService){} 新英雄:putDetail 数据源:MatTableDataSource; 恩戈尼尼特(){ this.appService.getComplaints() .subscribe(data=>this.newHero=data); this.dataSource=this.newHero; } 错误: 类型“putDetail”不可分配给类型 “MatTableDataSource”。类型中缺少属性“\u data” “细节”。(属
您需要初始化
dataSource
并使用this.dataSource.data
而不是this.dataSource
,并将其放入subscribe
回调中,以确保仅在解决订阅时才填充它
从http
得到的响应不是JSON数组,而是JSON对象。使用将响应转换为数组
constructor(private appService: AppService) { }
newHero:putDetail
dataSource = new MatTableDataSource<putDetail>();
ngOnInit() {
this.appService.getcomplains()
.subscribe(data{ => this.newHero = data;
this.dataSource.data = Object.values(this.newHero);
});
}
构造函数(私有appService:appService){}
新英雄:putDetail
dataSource=新MatTableDataSource();
恩戈尼尼特(){
this.appService.getComplaints()
.subscribe(数据{=>this.newHero=data;
this.dataSource.data=Object.values(this.newHero);
});
}
我仍然收到相同的错误,类型“putDetail”不可分配给类型“putDetail[]”。类型“putDetail”中缺少属性“includes”。如果此操作无效,是否共享您的服务代码?您的服务是否返回对象数组?服务代码:getcomplains():Observable{返回this.http.get(');}更新了我的回答是的,我添加了快照,请检查