角材料组件在我的项目中不起作用

时间:2020-10-22 09:21:01

标签: angular typescript

我正在尝试向项目中添加角度材质,但是由于某种原因,它似乎不起作用。 我收到此错误:


    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.

除了我要导入MatFormFieldModule。

角度版本:10.1.4 角材料版本10.2.4

这是我的共享模块文件,该文件导入和导出我的Material模块(我什至尝试添加CUSTOM ELEMENTS SCHEMA,但没有做任何更改):

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

这是我的登录模块:

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

登录组件模板:

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

我的应用模块:

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

我正在使用ng packagr将组件从公共库导出到该项目中,该项目还包含Angular Material,但我还没有导入任何组件,仅导入了样式。

1 个答案:

答案 0 :(得分:1)

我看到您仍然没有将LoginModule导入AppModule。 如果将ShareModule导入到AppModule。您不需要在LoginModule中导入