【发布时间】:2019-04-30 08:08:03
【问题描述】:
我正在尝试在 Ionic 4 项目中使用 Angular Material。我已经通过 NPM 安装了所有需要的依赖项。为了获得更干净和可维护的代码,我创建了一个单独的ngModule,它按照 Angular Material Docs(入门部分)导入了我在应用程序中需要的所有组件。
然后,我将它导入到我想要使用它的页面模块中。
但是,当我想启动材质组件时,似乎没有使用导入,所以我无法使用该组件。
我尝试在 app.module.ts 文件中直接导入材料组件模块,结果相同。我可以直接导入每个我想使用的组件,而不是包含所有组件的模块,但我真的很想知道为什么这种方法不起作用,以及是否有什么我做错了。
这就是我的自定义模块的外观:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatButtonModule} from '@angular/material';
import {MatDialogModule} from '@angular/material/dialog';
@NgModule({
declarations: [],
imports: [
CommonModule,
BrowserAnimationsModule,
MatButtonModule,
MatDialogModule
],
exports: [
MatDialogModule,
MatButtonModule
]
})
export class AppMaterialModule { }
这就是我尝试将它导入到我想使用它的地方的方式:
import { AppMaterialModule} from '../../app-material.module';
@NgModule({
declarations: [],
imports: [
AppMaterialModule,
....
]
...
我希望能够使用在AppMaterialModule 中导入的所有材料组件,仅包括我当时正在使用的这个模块,但不知何故这种方法不起作用。
感谢您的帮助。
【问题讨论】:
-
你有什么错误吗?
-
@Jota.Toledo 如果我尝试使用例如分页器组件:
'mat-paginator' is not a known element: 1. If 'mat-paginator' is an Angular component, then verify that it is part of this module. 2. If 'mat-paginator' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. -
对于 Angular 9+ 检查这个答案:stackoverflow.com/questions/62295166/…
标签: angular ionic-framework angular-material ionic4