【问题标题】:Angular Material MatTableDataSource cannot be found找不到角度材质 MatTableDataSource
【发布时间】:2017-11-20 11:15:45
【问题描述】:

我正在尝试使用Angular Material Table 创建一个表,在示例中总是有导入:

import {MatTableDataSource} from '@angular/material';

我正在尝试做同样的事情,但我收到一个错误:“模块'../node_modules/@angular/material/material'没有导出成员MatTableDataSource。

我有什么遗漏吗?

这是 package.json 版本:

"dependencies": {
 "@angular/animations": "^4.4.6",
 "@angular/cdk": "^2.0.0-beta.12",
 "@angular/common": "^4.2.4",
 "@angular/compiler": "^4.2.4",
 "@angular/core": "^4.2.4",
 "@angular/forms": "^4.2.4",
 "@angular/http": "^4.2.4",
 "@angular/material": "^2.0.0-beta.12",
 "@angular/platform-browser": "^4.2.4",
 "@angular/platform-browser-dynamic": "^4.2.4",
 "@angular/router": "^4.2.4",
 "bootstrap": "^3.3.7",
 "core-js": "^2.4.1",
 "pdfmake": "^0.1.33",
 "rxjs": "^5.4.2",
 "zone.js": "^0.8.14"
},
"devDependencies": {
 "@angular/cli": "1.4.9",
 "@angular/compiler-cli": "^4.2.4",
 "@angular/language-service": "^4.2.4",
 "@types/jasmine": "~2.5.53",
 "@types/jasminewd2": "~2.0.2",
 "@types/node": "~6.0.60",
 "codelyzer": "~3.2.0",
 "jasmine-core": "~2.6.2",
 "jasmine-spec-reporter": "~4.1.0",
 "karma": "~1.7.0",
 "karma-chrome-launcher": "~2.1.1",
 "karma-cli": "~1.0.1",
 "karma-coverage-istanbul-reporter": "^1.2.1",
 "karma-jasmine": "~1.1.0",
 "karma-jasmine-html-reporter": "^0.2.2",
 "protractor": "~5.1.2",
 "ts-node": "~3.2.0",
 "tslint": "~5.7.0",
 "typescript": "~2.3.3"
}

【问题讨论】:

  • 正在尝试迁移到 cdk 和材料的 5.0.0-rc.0 版本。
  • @R.Richards 我必须升级到 Angular 5,然后将所有依赖项升级到最新版本。感谢您的帮助!

标签: angular angular-material2


【解决方案1】:

我遇到了同样的问题。事实证明,MatTableDataSource 是 Angular Material 5.0.0-rc0 中的新功能。 我将角度 4 更新为角度 5,并将角度材料更新为 5.0.0-rc0。 (参考:https://github.com/angular/material2/releases

如果有帮助,这是我的 package.json 文件。

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "build": "tsc -p src/",
    "build:watch": "tsc -p src/ -w",
    "build:e2e": "tsc -p e2e/",
    "serve": "lite-server -c=bs-config.json",
    "serve:e2e": "lite-server -c=bs-config.e2e.json",
    "prestart": "npm run build",
    "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
    "pree2e": "npm run build:e2e",
    "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
    "preprotractor": "webdriver-manager update",
    "protractor": "protractor protractor.config.js",
    "pretest": "npm run build",
    "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
    "pretest:once": "npm run build",
    "test:once": "karma start karma.conf.js --single-run",
    "lint": "tslint ./src/**/*.ts -t verbose"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@angular/animations": "^5.0.2",
    "@angular/cdk": "^5.0.0-rc0",
    "@angular/common": "^5.0.2",
    "@angular/compiler": "^5.0.2",
    "@angular/core": "^5.0.2",
    "@angular/forms": "^5.0.2",
    "@angular/http": "^5.0.2",
    "@angular/material": "^5.0.0-rc0",
    "@angular/platform-browser": "^5.0.2",
    "@angular/platform-browser-dynamic": "^5.0.2",
    "@angular/router": "^5.0.2",
    "angular-in-memory-web-api": "~0.5.1",
    "angular-material": "^1.1.5",
    "core-js": "^2.5.1",
    "hammerjs": "^2.0.8",
    "rxjs": "^5.5.2",
    "systemjs": "0.20.19",
    "zone.js": "^0.8.18"
  },
  "devDependencies": {
    "concurrently": "^3.5.0",
    "lite-server": "^2.3.0",
    "typescript": "~2.6.0",
    "canonical-path": "0.0.2",
    "tslint": "^5.8.0",
    "lodash": "^4.17.4",
    "jasmine-core": "~2.8.0",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.2.0",
    "rimraf": "^2.6.2",
    "@types/node": "^6.0.46",
    "@types/jasmine": "2.5.36"
  },
  "repository": {}
}

【讨论】:

  • 这是正确答案,为我解决了问题。
  • 更具体地了解您所做的更改会对我有所帮助....
【解决方案2】:

只需在“材料”后添加文件夹名称即可导入,如下所示,

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

这已经解决了问题。希望它能解决您的问题

【讨论】:

    【解决方案3】:

    我遇到了同样的错误,我有两个解决方案要提出

    1。保持相同的 package.json(使用 "@angular/material": "^2.0.0-beta.12")并尝试实现此

    [通过以下方式找到解决方案:https://github.com/angular/material2/issues/6036]

    X.component.html:

    <mat-table #table [dataSource]="dataSource">
    <ng-container matColumnDef="position">
      <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
    </ng-container>
    
    <!-- Name Column -->
    <ng-container matColumnDef="name">
      <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
    </ng-container>
    
    <!-- Weight Column -->
    <ng-container matColumnDef="weight">
      <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
    </ng-container>
    
    <!-- Color Column -->
    <ng-container matColumnDef="symbol">
      <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
    </ng-container>
    
    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-table>
    

    X.component.ts:

    import { DataSource } from '@angular/cdk/collections';
    
    @Component({...
    })
    export class XComponent implements OnInit {
    
    displayedColumns = ['position', 'name', 'weight', 'symbol'];
      dataSource: eDataSource;
      ELEMENT_DATA: Element[] = [
      {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
      {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
      {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
      {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
      {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
      {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
      {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
      {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
      {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
      {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
      {position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'},
      {position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'},
      {position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'},
      {position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'},
      {position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'},
      {position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'},
      {position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'},
      {position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'},
      {position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'},
      {position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'},
      ];
    
      ngOnInit() {
        this.dataSource = new eDataSource(this.ELEMENT_DATA);
      }
    }
    
    export interface Element {
      name: string;
      position: number;
      weight: number;
      symbol: string;
    }
    
    export class eDataSource extends DataSource<any> {
    
      constructor(private element: Element[]) {
        super();
      }
    
      connect(): Observable<Element[]> {
        return Observable.of(this.element);
      }
    
      disconnect() {}
    }
    

    2。使用此 package.json 构建另一个应用程序:

    {
      "name": "design2",
      "version": "0.0.0",
      "license": "MIT",
      "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
      },
      "private": true,
      "dependencies": {
        "@angular/animations": "^5.0.0",
        "@angular/cdk": "^5.0.0-rc0",
        "@angular/common": "^5.0.0",
        "@angular/compiler": "^5.0.0",
        "@angular/core": "^5.0.0",
        "@angular/forms": "^5.0.0",
        "@angular/http": "^5.0.0",
        "@angular/material": "^5.0.0-rc0",
        "@angular/platform-browser": "^5.0.0",
        "@angular/platform-browser-dynamic": "^5.0.0",
        "@angular/router": "^5.0.0",
        "core-js": "^2.4.1",
        "rxjs": "^5.5.2",
        "zone.js": "^0.8.14"
      },
      "devDependencies": {
        "@angular/cli": "1.5.3",
        "@angular/compiler-cli": "^5.0.0",
        "@angular/language-service": "^5.0.0",
        "@types/jasmine": "~2.5.53",
        "@types/jasminewd2": "~2.0.2",
        "@types/node": "~6.0.60",
        "codelyzer": "^4.0.1",
        "jasmine-core": "~2.6.2",
        "jasmine-spec-reporter": "~4.1.0",
        "karma": "~1.7.0",
        "karma-chrome-launcher": "~2.1.1",
        "karma-cli": "~1.0.1",
        "karma-coverage-istanbul-reporter": "^1.2.1",
        "karma-jasmine": "~1.1.0",
        "karma-jasmine-html-reporter": "^0.2.2",
        "protractor": "~5.1.2",
        "ts-node": "~3.2.0",
        "tslint": "~5.7.0",
        "typescript": "~2.4.2"
      }
    }
    

    现在它与来自https://material.angular.io/components/table/overview 的示例完美配合。

    我的 app.module.ts:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { MatTableModule } from '@angular/material/table';
    
    import { AppComponent } from './app.component';
    
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        MatTableModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    我的 X.component.ts:

    import { Component } from '@angular/core';
    import { MatTableDataSource } from '@angular/material/table';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title = 'app';
      displayedColumns = ['position', 'name', 'weight', 'symbol'];
      dataSource = new MatTableDataSource<Element>(ELEMENT_DATA);
    }
    
    export interface Element {
      name: string;
      position: number;
      weight: number;
      symbol: string;
    }
    
    const ELEMENT_DATA: Element[] = [
      {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
      {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
      {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
      {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
      {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
      {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
      {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
      {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
      {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
      {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
      {position: 11, name: 'Sodium', weight: 22.9897, symbol: 'Na'},
      {position: 12, name: 'Magnesium', weight: 24.305, symbol: 'Mg'},
      {position: 13, name: 'Aluminum', weight: 26.9815, symbol: 'Al'},
      {position: 14, name: 'Silicon', weight: 28.0855, symbol: 'Si'},
      {position: 15, name: 'Phosphorus', weight: 30.9738, symbol: 'P'},
      {position: 16, name: 'Sulfur', weight: 32.065, symbol: 'S'},
      {position: 17, name: 'Chlorine', weight: 35.453, symbol: 'Cl'},
      {position: 18, name: 'Argon', weight: 39.948, symbol: 'Ar'},
      {position: 19, name: 'Potassium', weight: 39.0983, symbol: 'K'},
      {position: 20, name: 'Calcium', weight: 40.078, symbol: 'Ca'},
    ];
    

    还有我的 X.component.html:

    <mat-table #table [dataSource]="dataSource">
      <ng-container matColumnDef="position">
        <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
      </ng-container>
      <ng-container matColumnDef="name">
        <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
      </ng-container>
      <ng-container matColumnDef="weight">
        <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
      </ng-container>
      <ng-container matColumnDef="symbol">
        <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
        <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
      </ng-container>
      <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
      <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
    </mat-table>
    

    好的:)

    【讨论】:

    • 非常感谢,解决方案 2 对我有用。我更改了 package.json,然后使用“npm install”构建了项目。
    猜你喜欢
    • 2019-06-14
    • 2018-04-25
    • 2022-10-05
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2016-04-30
    • 2016-01-11
    • 1970-01-01
    相关资源
    最近更新 更多