【问题标题】:Error: No component factory found for MatSnackBarContainer错误:未找到 MatSnackBarContainer 的组件工厂
【发布时间】:2019-01-03 03:17:28
【问题描述】:

我已经开始了一个简单的 Angular Material 项目,并尝试在页面加载时显示 Snack-bar。但它抛出以下错误:-

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

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

这是我的 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"
  }

如何解决?

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:
    • 在使用 SnackBar 模块之前,您必须在 app.module.ts 中添加 MatSnackBarModule
    import { MatSnackBarModule } from '@angular/material';
    
    • 在导入数组中添加MatSnackBarModule

    • 您必须在declarationsbootstrap 数组中添加相关组件(您正在使用snackbar 组件)

     @NgModule({
       declarations: [
        AppComponent  <-- here
      ],
    
     imports: [
       MatSnackBarModule  <-- here
     ],
    
      bootstrap: [
        AppComponent  <-- here
      ],
    })
    export class AppModule { }
    

    这里是Working StackBlitz

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 2020-07-01
      • 2019-07-10
      • 2019-02-18
      • 2019-03-13
      • 2020-03-08
      • 2018-05-06
      • 2017-02-15
      • 1970-01-01
      相关资源
      最近更新 更多