Reputation: 4718
I'm trying to get Angular Material to work within an Angular Library.
These are the steps I have taken:
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>
I now run the app and I receive the following error:
'mat-checkbox' is not a known element: 1. If 'mat-checkbox' is an Angular component, then verify that it is part of this module.
This is obviously the standard message when a reference is missing but as I've added them I'm wondering what else I need to do within a library to get this to work?
Thanks
Upvotes: 4
Views: 3252
Reputation: 933
I tried all the steps mentioned in the question by you and I could run the application without any error. However, I need to update some of your code as below:
import { TestLibModule } from 'test-lib'; // <-- this
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
TestLibModule // <-- this
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
<lib-test-lib></lib-test-lib>
No change required
<mat-checkbox>my checkbox</mat-checkbox>
Upvotes: 4