我已经从教程中创建了一个基本的模态对话框,但有点卡住了,加载我的项目可以解决此错误:
ERROR Error: Uncaught (in promise): Error: inject() must be called from an injection context
Error: inject() must be called from an injection context
at injectInjectorOnly (core.js:728)
我不确定为什么错误消息显示的是小写字母i inject()而不是我在构造函数中调用的大写字母I @Inject
import { Component, NgModule, OnInit } from '@angular/core';
import { Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MatDialogConfig, MatDialogModule, MAT_DIALOG_DATA } from '@angular/material/dialog';
@Component({
selector: 'term-component',
templateUrl: './term.component.html'
})
@NgModule({
imports: [MatDialog, MatDialogRef, MatDialogConfig, MatDialogModule],
providers: [{
provide: MatDialog, useValue: {}
}]
})
export class TermDialog implements OnInit {
constructor(
@Inject(MAT_DIALOG_DATA) private modalData: any,
public dialogRef: MatDialogRef<TermDialog>
) {
console.log(this.modalData);
}
ngOnInit() { alert(''); }
actionFunction() {
this.closeModal();
}
closeModal() {
this.dialogRef.close();
}
onNoClick(): void {
this.dialogRef.close();
}
}
所有类似的帮助主题都表示要确保我正在使用大写字母@Inject而不是inject。但是,我确实看到了一个建议,是从'@ angular / core / testing'而不是'@ angular / core'导入Inject,我认为这是不可能的?
任何帮助将不胜感激,我之前从未在Hello World项目中被困过那么多次。
新:
好的,我想我已经满足了所有5个要求,但仍然看到相同的错误。 这是我的app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { OverlayModule } from '@angular/cdk/overlay';
import { MatDialogModule } from '@angular/material/dialog';
import { AmplifyAngularModule, AmplifyService } from 'aws-amplify-angular';
import { AppComponent } from './app.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component';
import { CounterComponent } from './counter/counter.component';
import { FetchDataComponent } from './fetch-data/fetch-data.component';
import { TermComponent } from './term.component';
import { TermDialog } from './term-dialog.component';
import { LoginComponent } from './login/login.component';
@NgModule({
declarations: [
AppComponent,
NavMenuComponent,
HomeComponent,
CounterComponent,
FetchDataComponent,
TermComponent,
LoginComponent,
TermDialog
],
imports: [
AmplifyAngularModule,
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
FormsModule,
OverlayModule,
MatDialogModule,
RouterModule.forRoot([
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: 'term', component: TermComponent },
{ path: '**', component: LoginComponent }
])
],
entryComponents: [
TermDialog
],
providers: [
AmplifyService
],
bootstrap: [AppComponent]
})
export class AppModule { }
和我的term.component文件,该文件应该启动term-dialog
import { Component, NgModule } from '@angular/core';
import { MatDialog, MatDialogConfig, MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { TermService } from '../../services/term.service';
import { TermDialog } from './term-dialog.component';
import { compileNgModule } from '@angular/compiler';
@Component({
selector: 'term-component',
templateUrl: './term.component.html'
})
@NgModule({
imports: [MatDialogModule],
providers: [{ provide: MatDialog, useValue: {}
}]
})
export class TermComponent {
clicked: boolean;
success: boolean;
constructor(public dialog: MatDialog, private termService: TermService) {
this.clicked = false;
}
openAddNewTermDialog() {
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.height = "350px";
dialogConfig.width = "600px";
dialogConfig.data = {
};
const dialogRef = this.dialog.open(TermDialog, dialogConfig);
dialogRef.afterClosed().subscribe(result => {
console.log('closed dialog');
this.success = result;
})
}
}
答案 0 :(得分:1)
要使mat-dialog
正常工作,我们必须满足以下几点。
#1 声明对应于dialog
的{{1}}组件。
module
#2 从父组件中使用类型为@NgModule({
imports: [
// ...
MatDialogModule
],
declarations: [
AppComponent,
ExampleDialogComponent
],
entryComponents: [
ExampleDialogComponent
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
的变量调用mat-dialog
。
MatDialog
#3 在对话框组件上声明public dialog: MatDialog
,以访问其方法。
dialogRef
#4 如果想将某些数据从父组件传递到对话框组件,请在对话框组件上声明@Component({/* ... */})
export class YourDialog {
constructor(public dialogRef: MatDialogRef<YourDialog>) { }
closeDialog() {
this.dialogRef.close('Pizza!');
}
}
。
@Inject
#5 在import {Component, Inject} from '@angular/core';
import {MAT_DIALOG_DATA} from '@angular/material/dialog';
@Component({
selector: 'your-dialog',
template: 'passed in {{ data.name }}',
})
export class YourDialog {
constructor(@Inject(MAT_DIALOG_DATA) public data: any) { }
}
定义中将组件声明为entryComponents。
Ref-https://medium.com/@ppm1988/matdialog-not-displaying-properly-b3c4cf19ec4
我发现您已经处理了第3点和第4点。也尝试修复其他点。
如果您尚未添加
module
模块,那么您可能还想添加它。
确保应用程序已加载
animations
主题。
答案 1 :(得分:0)
TermDialog没有单独的HTML。它指的是TermComponent的模板和选择器。
我已经注释掉了演示代码中与解决方案无关的一些语句。
动画
还将BrowserAnimationsModule
添加到app.module
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
@NgModule({
// ...
imports: [BrowserAnimationsModule],
// ...
})
主题 我想您也已经注意了材料主题。
<link href="https://unpkg.com/@angular/material/core/theming/prebuilt/indigo-pink.css" rel="stylesheet">
答案 2 :(得分:0)
好吧,事实证明答案是根本不使用MatDialogs,而只是使用TermDialogComponent的选择器标记将TermDialogComponent html注入基本的模式对话框中
所以我在我的term.component.html中添加了用于模式对话框的html,并简单地粘贴了
<term-dialog-component></term-dialog-component>
,一切都按预期进行,没有大惊小怪,添加了任何模块/库。我可能永远不知道为什么我无法使基本的模态MatDialog正常工作,但是我为此感到安心。对于任何有相同问题的人,我的建议是不要使用MatDialogs,这是不值得的,而且怪异的制表符动画还是很有趣的。
感谢大家的回应和时间