L. G.
L. G.

Reputation: 35

Angular material components don't work on my project

I'm trying to add angular material to my project but it doesn't seem to work for some reason. I get this error:


    ERROR in src/app/login/components/login/login.component.html:2:1 - error NG8001: 'mat-form-field' is not a known element:
    1. If 'mat-form-field' is an Angular component, then verify that it is part of this module.
    2. If 'mat-form-field' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

    2 <mat-form-field appearance="outline">
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      src/app/login/components/login/login.component.ts:5:16
        5   templateUrl: './login.component.html',
                         ~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component LoginComponent.

Except I am importing the MatFormFieldModule.

Angular version: 10.1.4 Angular material version 10.2.4

Here is my shared module file which imports and exports my Material modules (I even tried adding the CUSTOM ELEMENTS SCHEMA but it didn't change anything):

import { NgModule } from '@angular/core';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    MatInputModule,
    MatFormFieldModule
  ],
  exports: [
    CommonModule,
    MatInputModule,
    MatFormFieldModule
  ],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA
  ]
})
export class SharedModule { }

And here my login module:

import { NgModule } from '@angular/core';
import { LoginComponent } from './components/login/login.component';
import { SharedModule } from '../shared/shared.module';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

@NgModule({
  declarations: [LoginComponent],
  imports: [
    SharedModule
  ],
  schemas: [
    CUSTOM_ELEMENTS_SCHEMA
  ]
})
export class LoginModule { }

Login component template:

<mat-form-field appearance="outline">
    <input matInput placeholder="Placeholder">
</mat-form-field>

And my app module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { SharedModule } from './shared/shared.module';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    SharedModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

I'm using ng packagr to export components from a common library into this projects which also contains Angular Material but I haven't imported any of the components yet to it, only the styles.

Upvotes: 1

Views: 1482

Answers (1)

dien luc
dien luc

Reputation: 26

I see you still do not import LoginModule to AppModule. If you imported ShareModule to AppModule. You don't need import in LoginModule

Upvotes: 1

Related Questions