Los componentes de material angular no funcionan en mi proyecto

Oct 22 2020

Estoy tratando de agregar material angular a mi proyecto, pero parece que no funciona por alguna razón. Me sale este 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.

Excepto que estoy importando MatFormFieldModule.

Versión angular: 10.1.4 Versión material angular 10.2.4

Aquí está mi archivo de módulo compartido que importa y exporta mis módulos de Material (incluso intenté agregar el ESQUEMA DE ELEMENTOS PERSONALIZADOS pero no cambió nada):

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 { }

Y aquí mi módulo de inicio de sesión:

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 { }

Plantilla de componente de inicio de sesión:

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

Y mi módulo de aplicación:

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 { }

Estoy usando ng packagr para exportar componentes de una biblioteca común a estos proyectos que también contienen Material angular, pero aún no he importado ninguno de los componentes, solo los estilos.

Respuestas

1 dienluc Oct 22 2020 at 16:38

Veo que todavía no importa LoginModule a AppModule. Si importó ShareModule a AppModule. No necesita importar en LoginModule