【问题标题】:how can i fix this error Can't bind to 'dataSource' since it isn't a known property of 'mat-table'我如何修复此错误无法绑定到 \'dataSource\' 因为它不是 \'mat-table\' 的已知属性
【发布时间】:2022-11-27 21:47:33
【问题描述】:

无法绑定到“dataSource”,因为它不是“mat-table”的已知属性。

  1. 如果“mat-table”是一个 Angular 组件并且它有“dataSource”输入,则验证它是该模块的一部分。
  2. 如果“mat-table”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到此组件的“@NgModule.schemas”以抑制此消息。
  3. 要允许任何属性,请将“NO_ERRORS_SCHEMA”添加到此组件的“@NgModule.schemas”。

    这是我的 app.component.html 文件

    <mat-table #table [dataSource]="dataSource" >
    
      <!-- Checkbox Column -->
      <ng-container matColumnDef="select">
        <th mat-header-cell *matHeaderCellDef>
          <mat-checkbox (change)="$event ? toggleAllRows() : null"
                        [checked]="selection.hasValue() && isAllSelected()"
                        [indeterminate]="selection.hasValue() && !isAllSelected()"
                        [aria-label]="checkboxLabel()">
          </mat-checkbox>
        </th>
        <td mat-cell *matCellDef="let row">
          <mat-checkbox (click)="$event.stopPropagation()"
                        (change)="$event ? selection.toggle(row) : null"
                        [checked]="selection.isSelected(row)"
                        [aria-label]="checkboxLabel(row)">
          </mat-checkbox>
        </td>
      </ng-container>
    
      <!-- Position Column -->
      <ng-container matColumnDef="position">
        <th mat-header-cell *matHeaderCellDef> No. </th>
        <td mat-cell *matCellDef="let element"> {{element.position}} </td>
      </ng-container>
    
      <!-- Name Column -->
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef> Name </th>
        <td mat-cell *matCellDef="let element"> {{element.name}} </td>
      </ng-container>
    
      <!-- Weight Column -->
      <ng-container matColumnDef="weight">
        <th mat-header-cell *matHeaderCellDef> Weight </th>
        <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
      </ng-container>
    
      <!-- Symbol Column -->
      <ng-container matColumnDef="symbol">
        <th mat-header-cell *matHeaderCellDef> Symbol </th>
        <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
      </ng-container>
    
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"
          (click)="selection.toggle(row)">
      </tr>
    </mat-table>
    
    

【问题讨论】:

标签: angular angular-material


【解决方案1】:

首先,将 MatModule 添加到 app.module.ts 中:

import { MatTableModule } from '@angular/material/table'

在 app.module.ts 的导入数组中:

imports: [
    CommonModule,
    MatTableModule <==
  ],

然后在你的 app.component.ts 中导入如下:

import { MatTableDataSource, MatSort } from '@angular/material';
import { DataSource } from '@angular/cdk/table';

然后它应该可以正常工作

【讨论】:

    【解决方案2】:

    我相信正确的方法是将它添加到 .ts 文件中

        ngAfterViewInit(): void {
        this.dataSource = new TablesDataSource(this.data);
        this.dataSource.sort = this.sort;
        this.dataSource.paginator = this.paginator;
        this.table.dataSource = this.dataSource;
    
      }
    

    【讨论】:

      猜你喜欢
      • 2019-04-10
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2020-03-11
      相关资源
      最近更新 更多