简体   繁体   English

TS2339:“行为主题”类型上不存在属性“排序”

[英]TS2339: Property 'sort' does not exist on type 'BehaviorSubject<AbstractControl[]

I am looking to create a dynamic table using angular material where i can add/remove data dynamically.我正在寻找使用角度材料创建一个动态表,我可以在其中动态添加/删除数据。 Being very new to Angular , i have referred follwing stackblitz to create the table.作为 Angular 的新手,我参考了以下 stackblitz 来创建表格。

https://stackblitz.com/edit/angular-material-editable-table-fazhbc https://stackblitz.com/edit/angular-material-editable-table-fazhbc

While all my changes work - i am not able to sort the data at虽然我的所有更改都有效 - 我无法对数据进行排序

 ngAfterViewInit() {
    this.dataSource.sort = this.matSort;
  }

The error i get TS2339: Property 'sort' does not exist on type 'BehaviorSubject<AbstractControl[]我得到的错误 TS2339: 类型'BehaviorSubject<AbstractControl[]上不存在属性 'sort'

is the sorting isn't supported like in material table data source or am i missing something ?是不是像材料表数据源那样不支持排序,还是我遗漏了什么?

您需要使用valuegetValue()从 BehaviorSubject 获取当前值,并按如下方式对其进行排序: this.dataSource.value.sort(this.matSort)

由于 BehaviorSubject 返回 observable 因此首先订阅该数据然后应用排序功能

You can think of this as a news reporter talking on TV and giving latest updates to all similarly Behaviour Subject pass latest data.您可以将其视为新闻记者在电视上交谈并为所有类似的行为主题传递最新数据提供最新更新。

Add one service as myService and add following code public myData: BehaviorSubject = new BehaviorSubject([]);添加一项服务作为 myService 并添加以下代码 public myData: BehaviorSubject = new BehaviorSubject([]); updateData(data): void { this.myData.next(data); updateData(data): void { this.myData.next(data); } }

then call updateDatafrom where you want to send data to others.然后从您想要向其他人发送数据的地方调用 updateData。 this.myService.updateData("Test"); this.myService.updateData("测试"); You can pass any type of data here like object,array etc. Then on other component where you want this data write following code this.myService.myData.subscribe((data) => { console.log(data); });您可以在此处传递任何类型的数据,如对象、数组等。然后在您希望此数据的其他组件上写入以下代码 this.myService.myData.subscribe((data) => { console.log(data); }); You will get data.你会得到数据。 You can write above in any component where you want data.您可以在需要数据的任何组件中编写上面的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TS2339:“AbstractControl”类型上不存在属性“控件” - TS2339: Property 'controls' does not exist on type 'AbstractControl' 为什么我收到一条错误消息,指出错误 TS2339:类型“AbstractControl”上不存在属性“控件” - Why do i get an error saying error TS2339: Property 'controls' does not exist on type 'AbstractControl' 打字稿:箭头功能-TS2339:类型“ {}”上不存在属性 - Typescript: Arrow function - TS2339: Property does not exist on type '{}' 错误TS2339:类型“无效”上不存在属性“ _ws” - error TS2339: Property '_ws' does not exist on type 'void' 错误 TS2339:属性“Connection”在类型“Navigator”上不存在 - Error TS2339: Property 'Connection' does not exist on type 'Navigator' 错误TS2339:类型“ {}”上不存在属性“ hometeam” - error TS2339: Property 'hometeam' does not exist on type '{}' 错误 TS2339:“订阅”类型上不存在属性“订阅” - error TS2339: Property 'subscribe' does not exist on type 'Subscription 错误 TS2339:属性“flatMap”在类型“any”上不存在 - error TS2339: Property 'flatMap' does not exist on type 'any 错误 TS2339:“响应”类型上不存在属性“结果” - error TS2339: Property 'results' does not exist on type 'Response' TS2339:类型“{}”上不存在属性“文本” - TS2339: Property 'text' does not exist on type '{}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM