Angular Material Table错误:无法绑定到'mdHeaderRowDef',因为它不是'md-header-row'的已知属性

Jos*_*ven 1 angular-material angular-datatables angular-material2 angular angular-cdk

我正在尝试将CDK数据表转换为Material Design样式的数据表(请参阅:https://material.angular.io/components/table/overview),但是当我将cdk前缀更改为md时,我得到以下错误...


未捕获错误:模板解析错误:无法绑定到'mdHeaderRowDef',因为它不是'md-header-row'的已知属性.1.如果'md-header-row'是一个Angular组件并且它有'mdHeaderRowDef'输入,那么请验证它是否是该模块的一部分.2.如果'md-header-row'是Web组件,则将'CUSTOM_ELEMENTS_SCHEMA'添加到此组件的'@NgModule.schemas'以禁止显示此消息.3.要允许任何属性将"NO_ERRORS_SCHEMA"添加到此组件的"@NgModule.schemas".


我在网上发现的每个答案都告诉我,我需要导入CdkTableModule,但我已经这样做了&cdk表工作得很好.

import {Component, OnInit, ViewChild} from '@angular/core';
import {DataSource} from '@angular/cdk';
import { CdkTableModule } from '@angular/cdk';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/first';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/observable/merge';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/interval';
import 'rxjs/add/operator/map';
Run Code Online (Sandbox Code Playgroud)

我正在导入CdkTableModule,当我使用cdk前缀时,表格按预期显示...

<md-table [dataSource]="dataSource">

  <ng-container cdkColumnDef="number">
    <md-header-cell *cdkHeaderCellDef> number </md-header-cell>
    <md-cell *cdkCellDef="let element"><a routerLink="{{element.number}}"> {{element.number}} </a></md-cell>
  </ng-container>

  <ng-container cdkColumnDef="book">
    <md-header-cell *cdkHeaderCellDef> book </md-header-cell>
    <md-cell *cdkCellDef="let element"> {{element.book}} </md-cell>
  </ng-container>

  <md-header-row *cdkHeaderRowDef="['number', 'book']"></md-header-row>
  <md-row *cdkRowDef="let row; columns: ['number', 'book']"></md-row>
</md-table>
Run Code Online (Sandbox Code Playgroud)

结果:到目前为止一切都很好 ...... 在此输入图像描述 但如果我将"cdk"改为"md"

<md-table [dataSource]="dataSource">

  <ng-container mdColumnDef="number">
    <md-header-cell *mdHeaderCellDef> number </md-header-cell>
    <md-cell *mdCellDef="let element"><a routerLink="{{element.number}}"> {{element.number}} </a></md-cell>
  </ng-container>

  <ng-container mdColumnDef="book">
    <md-header-cell *mdHeaderCellDef> book </md-header-cell>
    <md-cell *mdCellDef="let element"> {{element.book}} </md-cell>
  </ng-container>

  <md-header-row *mdHeaderRowDef="['number', 'book']"></md-header-row>
  <md-row *mdRowDef="let row; columns: ['number', 'book']"></md-row>
</md-table>
Run Code Online (Sandbox Code Playgroud)

结果:破碎:( 在此输入图像描述

我已经尝试导入其他模块......包括MdTableModule和MdTable,但这也没有证明有用.有任何想法吗?

PS这是我的主要app.module.ts导入语句以防万一

imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
ReactiveFormsModule,
BrowserAnimationsModule,
MdAutocompleteModule,
MdButtonModule,
MdButtonToggleModule,
MdCardModule,
MdCheckboxModule,
MdChipsModule,
MdTableModule,
MdDatepickerModule,
MdDialogModule,
MdExpansionModule,
MdGridListModule,
MdIconModule,
MdInputModule,
MdListModule,
MdMenuModule,
MdCoreModule,
MdPaginatorModule,
MdProgressBarModule,
MdProgressSpinnerModule,
MdRadioModule,
MdRippleModule,
MdSelectModule,
MdSidenavModule,
MdSlideToggleModule,
MdSliderModule,
MdSnackBarModule,
MdSortModule,
MdTabsModule,
MdToolbarModule,
MdTooltipModule,
MdNativeDateModule,
CdkTableModule,
StyleModule
 ]
Run Code Online (Sandbox Code Playgroud)

Jos*_*ven 12

经过一小时的摆弄,我跑了

npm update --save

并将@ angular/cdk和@ angular/material更新为2.0.0-beta.10

在package.json中

 "@angular/material": "^2.0.0-beta.10",
 "@angular/cdk": "^2.0.0-beta.10",
Run Code Online (Sandbox Code Playgroud)

这打破了一些事情,所以我不得不四处挖掘并需要更新DataSource和CdkTableModule的导入

       import { CdkTableModule} from '@angular/cdk/table';
       import {DataSource} from '@angular/cdk/table';
Run Code Online (Sandbox Code Playgroud)

component.html

 <md-table [dataSource]="dataSource">

    <ng-container mdColumnDef="number">
      <md-header-cell *mdHeaderCellDef> number </md-header-cell>
      <md-cell *mdCellDef="let element"><a routerLink="{{element.number}}"> {{element.number}} </a></md-cell>
    </ng-container>

    <ng-container mdColumnDef="book">
      <md-header-cell *mdHeaderCellDef> book </md-header-cell>
      <md-cell *mdCellDef="let element"> {{element.book}} </md-cell>
    </ng-container>

    <ng-container mdColumnDef="s1">
      <md-header-cell *mdHeaderCellDef> S1 </md-header-cell>
      <md-cell *mdCellDef="let element"> {{element.sections[0] ? element.sections[0].type : ''}} {{element.sections[0] && element.sections[0].qs ? '('+element.sections[0].qs.length+')' : ''}}</md-cell>
    </ng-container>

    <ng-container mdColumnDef="s2">
      <md-header-cell *mdHeaderCellDef> S2 </md-header-cell>
      <md-cell *mdCellDef="let element"> {{element.sections[1] ? element.sections[1].type : ''}} {{element.sections[1] && element.sections[1].qs  ? '('+element.sections[1].qs.length+')' : ''}} </md-cell>
    </ng-container>

    <ng-container mdColumnDef="s3">
      <md-header-cell *mdHeaderCellDef> S3 </md-header-cell>
      <md-cell *mdCellDef="let element"> {{element.sections[2] ? element.sections[2].type : ''}} {{element.sections[2] && element.sections[2].qs ? '('+element.sections[2].qs.length+')' : ''}}</md-cell>
    </ng-container>

    <ng-container mdColumnDef="s4">
      <md-header-cell *mdHeaderCellDef> S4 </md-header-cell>
      <md-cell *mdCellDef="let element"> {{element.sections[3] ? element.sections[3].type : ''}} {{element.sections[3] && element.sections[3].qs ? '('+element.sections[3].qs.length+')' : ''}}</md-cell>
    </ng-container>

    <md-header-row *mdHeaderRowDef="['number', 'book', 's1', 's2', 's3', 's4']"></md-header-row>
    <md-row *mdRowDef="let row; columns: ['number', 'book', 's1', 's2', 's3', 's4']"></md-row>
  </md-table>
Run Code Online (Sandbox Code Playgroud)

component.ts

 import {Component, OnInit} from '@angular/core';

 // Data Table imports.
     import { DataSource } from '@angular/cdk/table';
     import { Observable } from 'rxjs/Observable';
     import 'rxjs/add/observable/of';

 @Component({
   selector: 'app-start',
   templateUrl: './start.component.html',
   styleUrls: ['./start.component.css']
 })
 export class StartComponent implements OnInit {

   pts: Pt[];
   dataSource = new ExampleDataSource(this.ptService);
   constructor( public ptService: PtService ) { }
   ngOnInit() {
     this.pt = this.ptService.getPts();
   }
 }

 export class ExampleDataSource extends DataSource<any> {
   data = this.ptService.getPts();
   constructor( public ptService: PtService ) { super() }
   /** Connect function called by the table to retrieve one stream containing the data to render. */
   connect(): Observable<Pt[]> {
     return Observable.of(this.data);
   }
   disconnect() {}
 }
Run Code Online (Sandbox Code Playgroud)

唷!

  • 请接受您的回答,以便其他人在遇到类似问题时也能受益 (2认同)

小智 6

MatTableModule在 app.module.ts 中导入

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

@NgModule({
  imports: [
     MatTableModule
  ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)