【问题标题】:Not able to assign datasource value in angular无法以角度分配数据源值
【发布时间】:2018-05-27 06:40:19
【问题描述】:

构造函数(私有appService:AppService){} newHero:put详情 数据源:MatTableDataSource; ngOnInit() { this.appService.getcomplains() .subscribe(data => this.newHero = data); this.dataSource = this.newHero; } 错误:

类型“putDetail”不可分配给类型 'MatTableDataSource'。类型中缺少属性“_data” 'putDetail'。 (属性)ComplaintsComponent.dataSource: MatTableDataSource

【问题讨论】:

    标签: angular material-design angular-material


    【解决方案1】:

    您需要初始化 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);
    
    
            });
    
          }
    

    【讨论】:

    • 我仍然遇到同样的错误,类型 'putDetail' 不能分配给类型 'putDetail[]'。 “putDetail”类型中缺少属性“includes”。
    • 如果这不起作用,请分享您的服务代码?您的服务是否返回对象数组?
    • 服务代码:getcomplains():Observable{ return this.http.get('localhost:7070/api/complains'); }
    • 更新了我的答案
    • 是的,我添加了快照,请检查
    猜你喜欢
    • 2021-06-19
    • 2018-02-24
    • 2018-08-27
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    相关资源
    最近更新 更多