我正在尝试让 Angular Material 在 Angular 库中工作。
这些是我采取的步骤:
ng new test-project
ng add @angular/material
ng g library test-lib
"peerDependencies": { "@angular/common": "^7.2.0", "@angular/core": "^7.2.0", "@angular/material": "^7.3.7" }
@Component({ selector: 'test-lib', template: ` <p> test! </p> <mat-checkbox>my checkbox</mat-checkbox> `, styles: [] })
ng build test-lib --watch
import { TestLibComponent } from 'test-lib' @NgModule({ declarations: [ AppComponent, TestLibComponent ], imports: [ BrowserModule, BrowserAnimationsModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
import { NgModule } from '@angular/core'; import { ControlLibraryComponent } from './control-library.component'; import { MatCheckboxModule } from '@angular/material/checkbox'; @NgModule({ declarations: [TestLibComponent], imports: [ MatCheckboxModule ], exports: [TestLibComponent] }) export class TestLibModule { }
<test-lib></test-lib>
我现在运行该应用程序并收到以下错误:
'mat-checkbox' 不是已知元素:
1. 如果 'mat-checkbox' 是一个 Angular 组件,那么验证它是这个模块的一部分。
这显然是缺少引用时的标准消息,但是当我添加它们时,我想知道在库中我还需要做什么才能使其正常工作?
谢谢
最佳答案
本地编码后更新答案:
我尝试了您在问题中提到的所有步骤,并且可以运行该应用程序而不会出现任何错误。但是,我需要更新您的一些代码,如下所示:
1. 应用程序模块
import { TestLibModule } from 'test-lib'; // <-- this
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
TestLibModule // <-- this
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
2.确保选择器名称
<lib-test-lib></lib-test-lib>
3. test-lib.module
无需更改
4.
<mat-checkbox>my checkbox</mat-checkbox>
截图5. 截图
关于angular - 让 Angular Material 在 Angular 库中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58217810/