gpt4 book ai didi

angular - 错误 : No component factory found for MatSnackBarContainer

转载 作者:行者123 更新时间:2023-12-01 21:59:46 24 4
gpt4 key购买 nike

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

ERROR Error: No component factory found for MatSnackBarContainer. Did you add it to @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"
}

如何解决?

最佳答案

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

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

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

imports: [
MatSnackBarModule <-- here
],

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

这是 Working StackBlitz

关于angular - 错误 : No component factory found for MatSnackBarContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54015887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com