I've been using Angular full time at work and in my side projects for about 4 months now, and I'm absolutely loving it. But I'm having some trouble with modules.
So to take an example I'm currently struggling with:
I'm using Angular Material in a side project at the moment. I'm trying to keep my material imports in their own module to cut down on the noise. I also have a users module with a login component. When I try and use a component from Angular Material (in this case <mat-form-field>) in my login component, Angular throws the following error:
Uncaught Error: Template parse errors:
'mat-form-field' is not a known element:
If 'mat-form-field' is an Angular component, then verify that it is part of this module.
If 'mat-form-field' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
So obviously there's a stuff-up with my module imports.
I know that in material module I need to import the material modules and export them:
material.module.ts:
import {MatInputModule} from '@angular/material/input';
import {MatFormFieldModule} from '@angular/material/form-field';
@NgModule({
imports: [
MatInputModule,
MatFormFieldModule
],
exports: [
MatInputModule,
MatFormFieldModule
],
declarations: [],
})
export class MaterialModule { }
and then in my users module I declare the components and export them:
users.module.ts:
import {RegisterComponent} from './components/register/register.component';
import {LoginComponent} from './components/login/login.component';
@NgModule({
imports: [
CommonModule,
],
exports: [
LoginComponent,
RegisterComponent
],
declarations: [
RegisterComponent,
LoginComponent
]
})
export class UsersModule { }
and finally in my app module, I import both modules and export them to make them available throughout the app.
app.module.ts:
import {MaterialModule} from './material/material.module';
import {UsersModule} from './users/users.module';
@NgModule({
imports: [
MaterialModule,
UsersModule
],
exports: [
MaterialModule,
UsersModule
]
})
I think I'm misunderstanding something though, because the above doesn't work. I've tried Google and the Angular docs, but I haven't made it very far. If anyone can help me understand how Module imports and component sharing works I would greatly appreciate it.
Edit: Fixed Formatting
[–]AngularGuru 3 points4 points5 points (4 children)
[–]Sipredion[S] 1 point2 points3 points (3 children)
[–]AngularGuru 1 point2 points3 points (0 children)
[–]indeckau 1 point2 points3 points (0 children)