无法以角度

时间:2018-05-27 06:40:19

标签: angular material-design angular-material

enter image description here构造函数(私有appService:AppService){}       newHero:putDetail       数据源:MatTableDataSource;       ngOnInit(){       this.appService.getcomplains()       .subscribe(data => this.newHero = data);       this.dataSource = this.newHero;       } 错误:

  

输入' putDetail'不能分配给类型   ' MatTableDataSource&#39 ;.物业' _data'在类型中缺失   ' putDetail&#39 ;. (property)ComplaintsComponent.dataSource:   MatTableDataSource

1 个答案:

答案 0 :(得分:2)

您需要初始化dataSource并使用this.dataSource.data代替this.dataSource并将其置于subscribe内回调,以确保仅在您的订阅解决时才填充在那之前。

http获得的响应不是JSON数组,而是JSON对象。使用 Object.values 将响应转换为数组。

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);


        });

      }