Angular 6'mat-button-toggle'不是已知元素

Nen*_*vic 6 angular-material angular

我检查过这些问题:

我已经按照本教程:

以前我使用过Angular Material,但为此它不会起作用:

compiler.js:1016未捕获错误:模板解析错误:'mat-button-toggle'不是已知元素:1.如果'mat-button-toggle'是Angular组件,则验证它是否是此模块的一部分.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { TRANSLATION_PROVIDERS } from './translations';
import { TranslateService } from './services/translator.service';
import { AppComponent } from './app.component';
import { MatButtonModule, MatCheckboxModule, MatFormFieldModule, MatInputModule, MatRippleModule, MatDatepickerModule, MatNativeDateModule } from '@angular/material';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule } from '@angular/forms';

import { ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';


@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    MatButtonModule,
    MatCheckboxModule,
    MatFormFieldModule,
    MatInputModule,
    MatRippleModule,
    BrowserAnimationsModule,
    MatDatepickerModule,
  ],
  exports: [
    BrowserModule,
    FormsModule,
    MatButtonModule,
    MatCheckboxModule,
    MatFormFieldModule,
    MatInputModule,
    MatRippleModule,
    BrowserAnimationsModule,
    MatDatepickerModule,
  ],
  declarations: [AppComponent],
  providers: [TRANSLATION_PROVIDERS, TranslateService],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

(这里我只想添加那些按钮,即使没有真正的功能)

....

  <div>
      <mat-button-toggle-group name="fontStyle" aria-label="Font Style">
          <mat-button-toggle value="bold">Bold</mat-button-toggle>
          <mat-button-toggle value="italic">Italic</mat-button-toggle>
          <mat-button-toggle value="underline">Underline</mat-button-toggle>
        </mat-button-toggle-group>
  </div>
Run Code Online (Sandbox Code Playgroud)

...

styles.css的

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

body { 
  font-family: Roboto, Arial, sans-serif;
  margin: 0;
}

.basic-container {
  padding: 30px;
}

.version-info {
  font-size: 8pt;
  float: right;
}

html, body { height: 100%; }
body { margin: 0; font-family: 'Roboto', sans-serif; }
Run Code Online (Sandbox Code Playgroud)

Sid*_*era 19

mat-button-toggle是作为一部分提供的,MatButtonToggleModule所以你也必须导入它.

在AppModule中为它添加一个import语句:

import {MatButtonToggleModule} from '@angular/material/button-toggle';
Run Code Online (Sandbox Code Playgroud)

然后将它添加到imports数组中,以便AppModule的模板可以理解它是什么mat-button-toggle.

另外,我不确定你为什么从AppModule导出这些模块.如果我们计划在其他模块中导入该模块,我们通常会从模块中导出任何内容.但由于这是AppModule,因此是你的RootModule,我认为你不会在任何其他模块中导入它.但情况可能并非如此.

  • 谢谢,这足以从“ @ angular / material”导入它;并添加到“导入”中。 (2认同)
  • 是的。我通常更喜欢按照其在官方网站上推荐的方式导入模块。https://material.angular.io/components/button-toggle/api这是他们的方法。不过只是我的偏爱。 (2认同)