【问题标题】:Angular 5 Material 5 MatTableDataSource..."'mat-card-title' is not a known element."Angular 5 Material 5 MatTableDataSource..."'mat-card-title' 不是已知元素。"
【发布时间】:2018-01-15 22:06:54
【问题描述】:

我正在尝试按照Material Documentation 中的描述实现<mat-table>

TL;DR:在将@angular/core、@angular/material 和@angular/CDK 升级到版本5 后,我突然不能使用mat-card 相关的标签了。

我无法正确导入 MatTableDataSource 的所有依赖项。我意识到我可以实现我自己的 DataSource like this,但如果它在文档中,那么在没有这个 hack 的情况下肯定可以进行工作配置。实现我自己的数据源也意味着丢失漂亮的可排序标题和过滤,这是我主要追求的。

我将项目升级到 Angular 5,认为 Material 也会升级,但事实并非如此。材料和 CDK 停留在“^2.0.0-beta.12”。

npm install 之后我看到了这些警告:

npm WARN @angular/cdk@5.0.4 requires a peer of @angular/core@~5.1.1 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/cdk@5.0.4 requires a peer of @angular/common@~5.1.1 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/material@5.0.4 requires a peer of @angular/core@~5.1.1 but none is installed. You must install peer dependencies yourself.
npm WARN @angular/material@5.0.4 requires a peer of @angular/common@~5.1.1 but none is installed. You must install peer dependencies yourself.
npm WARN @ng-bootstrap/ng-bootstrap@1.0.0-beta.6 requires a peer of @angular/core@^4.0.3 but none is installed. You must install peer dependencies yourself.
npm WARN @ng-bootstrap/ng-bootstrap@1.0.0-beta.6 requires a peer of @angular/common@^4.0.3 but none is installed. You must install peer dependencies yourself.
npm WARN @ng-bootstrap/ng-bootstrap@1.0.0-beta.6 requires a peer of @angular/forms@^4.0.3 but none is installed. You must install peer dependencies yourself.
npm WARN angular2-flash-messages@1.0.8 requires a peer of @angular/core@^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angular2-jwt@0.2.3 requires a peer of @angular/core@^2.0.0||^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN angular2-jwt@0.2.3 requires a peer of @angular/http@^2.0.0||^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@3.0.1 requires a peer of @angular/compiler@^2.3.1 || >=4.0.0-beta <5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@3.0.1 requires a peer of @angular/core@^2.3.1 || >=4.0.0-beta <5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

...但是 ng build 成功,并且我的应用程序的其余部分在 package.json 中运行良好:

   dependencies": {
        "@angular/animations": "^5.2.0",
        "@angular/cdk": "^2.0.0-beta.12",
        "@angular/common": "^5.2.0",
        "@angular/compiler": "^5.2.0",
        "@angular/core": "^5.2.0",
        "@angular/forms": "^5.2.0",
        "@angular/http": "^5.2.0",
        "@angular/material": "^2.0.0-beta.12",
        "@angular/platform-browser": "^5.2.0",
        "@angular/platform-browser-dynamic": "^5.2.0",
        "@angular/platform-server": "^5.2.0",
        "@angular/router": "^5.2.0",
        "@ng-bootstrap/ng-bootstrap": "1.0.0-beta.6",
        "angular-material-prefix-updater": "0.0.6",
        "angular2-flash-messages": "^1.0.8",
        "angular2-jwt": "^0.2.3",
        "core-js": "^2.5.3",
        "moment": "^2.20.1",
        "ng2-file-upload": "^1.3.0",
        "ngx-pagination": "^3.0.3",
        "quill": "^1.3.4",
        "rxjs": "^5.5.6",
        "zone.js": "^0.8.19"
      },
      "devDependencies": {
        "@angular/cli": "^1.6.3",
        "@angular/compiler-cli": "^5.2.0",
        "@angular/language-service": "^4.4.6",
        "@types/jasmine": "^2.8.3",
        "@types/jasminewd2": "^2.0.3",
        "@types/node": "^6.0.96",
        "codelyzer": "~3.0.1",
        "jasmine-core": "~2.6.2",
        "jasmine-spec-reporter": "~4.1.0",
        "karma": "^1.7.1",
        "karma-chrome-launcher": "~2.1.1",
        "karma-cli": "~1.0.1",
        "karma-coverage-istanbul-reporter": "^1.3.3",
        "karma-jasmine": "^1.1.1",
        "karma-jasmine-html-reporter": "^0.2.2",
        "protractor": "~5.1.2",
        "ts-node": "~3.0.4",
        "tslint": "~5.3.2",
        "typescript": "2.4.2",
        "webpack": "^3.10.0"
      } 

此时,我仍然无法导入 MatTableDataSource:

...node_modules/@angular/material/material"' has no exported member 'MatTableDataSource'.

...所以我增加了 Material 和 CDK 版本:

git diff package.json:

"dependencies": {
     "@angular/animations": "^5.2.0",
-    "@angular/cdk": "^2.0.0-beta.12",
+    "@angular/cdk": "^5.0.0-rc0",
     "@angular/common": "^5.2.0",
     "@angular/compiler": "^5.2.0",
     "@angular/core": "^5.2.0",
     "@angular/forms": "^5.2.0",
     "@angular/http": "^5.2.0",
-    "@angular/material": "^2.0.0-beta.12",
+    "@angular/material": "^5.0.0-rc0",
     "@angular/platform-browser": "^5.2.0",
     "@angular/platform-browser-dynamic": "^5.2.0",
     "@angular/platform-server": "^5.2.0",
.
.

现在我可以毫无问题地构建,但是当我的页面呈现时,我会在控制台中看到:

Uncaught Error: Template parse errors:
'mat-card-title' is not a known element:
1. If 'mat-card-title' is an Angular component, then verify that it is part of this module.
2. If 'mat-card-title' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

...现在我完全糊涂了。我在模板中使用的每个“未知”材料卡相关标签都有上述错误之一。我到底做错了什么,突然没有素材卡可以使用了?

【问题讨论】:

  • 你能在npm cache clean,npm install之后显示输出吗?
  • 你导入MatCardModule了吗?
  • Edric,解决方案是我没有导入 MatCardModule ......我的疏忽太简单了!仅供参考,当我使用较低版本的 Material 和 CDK 时,为什么 mat-card 标签可以正常工作的背后的概念是什么?其中一个需要显式导入 MatCardModule 的包发生了一些变化?

标签: angular material-design angular-material


【解决方案1】:

希望这对处于相同情况的人有所帮助:该解决方案与包依赖项没有直接关系。我没有在 @NgModule 导入中导入 MatCardModule。

如此明显的答案! ...但是,当我使用较低版本的 CDK 和 Material 时,我将不得不检查更改日志和文档以找出为什么以前不需要导入。模板解析错误仅在跳转到(更高)版本后才出现。

【讨论】:

  • 奇怪。我导入了它,并且还在@NgModule 中声明了我的组件,但是当我运行“npm run test”失败时仍然出现错误:模板解析错误:'mat-card-title' 不是已知元素:1. 如果'mat- card-title' 是一个 Angular 组件,然后验证它是这个模块的一部分。 2. 如果“mat-card-title”是一个 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。
  • 你有没有找到任何东西@Suisse
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 2020-07-02
  • 2020-03-14
  • 1970-01-01
相关资源
最近更新 更多