angular - Uncaught Error : Unexpected directive 'angular-material' imported by the module 'MyMaterialModule' . 请添加 @NgModule 注释。 Angular 5

标签 angular typescript

我一直有这个错误

Uncaught Error: Unexpected directive 'MatPaginator' imported by the module 'MaterialModule'. Please add a @NgModule annotation.



这是我的 Material 模块
import { NgModule, ViewChild } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatPaginator, MatSort, MatTableDataSource,MatButtonModule, 
MatTableModule } from '@angular/material';

@NgModule({
    imports: [MatButtonModule,MatTableModule,
              MatPaginator, MatSort, 
              MatTableDataSource],
    exports: [MatButtonModule,MatTableModule,MatPaginator, MatSort, 
             MatTableDataSource],
})
export class MaterialModule { }

这是我的 app.module
...
import { MaterialModule } from './material.module';


@NgModule({
  declarations: [
     ...

  ],
  imports: [
    ...
    MaterialModule,
    ...
  ],
  providers: [...],
  bootstrap: [ AppComponent]
})
export class AppModule { }

这也是我的观点
<div class="example-container mat-elevation-z8">
   <div class="example-header">
     <mat-form-field>
      <input matInput (keyup)="applyFilter($event.target.value)" 
    placeholder="Filter">
    </mat-form-field>
  </div>
<!-- Name Column -->
<ng-container matColumnDef="name">
  <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
  <mat-cell *matCellDef="let user"> {{user.name}} </mat-cell>
</ng-container>

<!-- Weight Column -->
<ng-container matColumnDef="email">
  <mat-header-cell *matHeaderCellDef> email </mat-header-cell>
  <mat-cell *matCellDef="let user"> {{user.email}} </mat-cell>
</ng-container>

<!-- Color Column -->
<ng-container matColumnDef="phone">
  <mat-header-cell *matHeaderCellDef> phone </mat-header-cell>
  <mat-cell *matCellDef="let user"> {{user.phone}} </mat-cell>
</ng-container>

<ng-container matColumnDef="company">
  <mat-header-cell *matHeaderCellDef> company </mat-header-cell>
  <mat-cell *matCellDef="let user"> {{user.company.name}} </mat-cell>
</ng-container>

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>


我从 angular material documentation我该如何解决?

最佳答案

我有同样的问题。我导入了 MatPaginatorModule在我的@NgModule进入导入数组。
它解决了我的问题。我希望它也适合你。

关于angular - Uncaught Error : Unexpected directive 'angular-material' imported by the module 'MyMaterialModule' . 请添加 @NgModule 注释。 Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471188/

相关文章:

Angular 2 英雄之旅 : Filter heroes by name

node.js - "npm run"卡在 ts-loader (webpack)

angular - 以 Angular 更改零件布线时重新加载组件

Angular(5) - 延迟加载和 canActivate 守卫 - StaticInjectorError

javascript - typescript 静态类

lambda - 将参数传递给 TypeScript 中的回调函数

typescript - 为什么 Typescript 类型检查器对这种未指定的类型感到不安?

html - 从外部资源覆盖 CSS

html - PrimeNG 表未采用 Angular 中的固定宽度

angular - 无法绑定(bind)到垫表中的数据