错误:未找到MatSnackBarContainer

时间:2019-01-03 03:17:29

标签: angular angular-material

我已经开始了一个简单的棱角材质项目,并尝试在页面加载时显示零食栏。但这会引发以下错误:-

  

错误错误:找不到MatSnackBarContainer的组件工厂。你加了吗       到@ NgModule.entryComponents?

我什至在app.module.ts的条目组件列表中都包含了AppComponent,但是它不起作用。

这是我的app.component.html:-

import { Component } from '@angular/core';
import { MatSnackBar } from '@angular/material';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [MatSnackBar]
})
export class AppComponent {
  title = 'snackbarapp';

  constructor(public snackBar: MatSnackBar) {
    this.snackBar.open('hello', 'world', { duration: 2000 });
  }
}

这是app.module.ts:-

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

import { AppComponent } from './app.component';
import { MatSnackBar } from '@angular/material';
import { Overlay } from '@angular/cdk/overlay';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule ],
  entryComponents: [AppComponent],
  providers: [MatSnackBar, Overlay],
  bootstrap: [AppComponent]
})
export class AppModule {}

我一直在依赖:-

 "dependencies": {
    "@angular/animations": "^6.1.10",
    "@angular/cdk": "^7.2.0",
    "@angular/common": "^6.1.0",
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",
    "@angular/material": "^7.2.0",
    "@angular/platform-browser": "^6.1.0",
    "@angular/platform-browser-dynamic": "^6.1.0",
    "@angular/router": "^6.1.0",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.7.0",
    "@angular/cli": "~6.1.5",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.1.0",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.4.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }

如何解决?

1 个答案:

答案 0 :(得分:2)

尝试在app.module.ts中添加MatSnackBarModule

import { MatSnackBarModule } from '@angular/material';

并在导入数组中添加MatSnackBarModule,您必须在declarationsbootstrap数组中添加相关组件:

@NgModule({
  declarations: [
    AppComponent  <-- here
  ],

  imports: [
    MatSnackBarModule  <-- here
  ],

  bootstrap: [
    AppComponent  <-- here
  ],
})
export class AppModule { }

这里是Working StackBlitz

相关问题