angular - NullInjectorError : No provider for MatDialogRef

原文 标签 angular typescript angular-material angular5

我无法像文档中描述的那样注入(inject) MatDialogRef:https://material.angular.io/components/dialog/overview

当我尝试这样做时,我遇到了错误:

错误错误:StaticInjectorError[MatDialogRef]:
静态注入(inject)器错误[MatDialogRef]:
NullInjectorError: 没有 MatDialogRef 的提供者!

app.module.ts

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

import {
	MatInputModule,
	MatDialogModule,
	MatProgressSpinnerModule,
	MatButtonModule,
	MatDialog,
	MatDialogRef
} from '@angular/material';

import { ApiModule } from '../api/api.module';
import { RoutingModule } from '../routing/routing.module';

import { RegistrationComponent } from './components/registration.component';
import { LoginComponent } from './components/login.component';

import { AccountService } from './services/account.service';

@NgModule({
	imports: [
		BrowserModule,
		MatInputModule,
		MatDialogModule,
		MatProgressSpinnerModule,
		MatButtonModule,
		FormsModule,
		RoutingModule,
		ApiModule
	],
	declarations: [
		RegistrationComponent,
		LoginComponent
	],
	entryComponents: [
		LoginComponent,
		RegistrationComponent
	],
	providers: [
		AccountService,
		MatDialog,
		MatDialogRef
	]
})
export class AccountModule {}


主页.component.ts

import { Component } from '@angular/core';

import { MatDialog } from '@angular/material';
import { RegistrationComponent } from '../account/components/registration.component';

@Component({
    moduleId: module.id.replace('compiled', 'app'),
    templateUrl: 'home.component.html'
})
export class HomeComponent
{
    constructor(private modalService: MatDialog) {}

    public openModal() : void
    {
        let dialog = this.modalService.open(RegistrationComponent, {});
    }
}


注册.component.ts

import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { MatDialogRef } from '@angular/material/dialog';

import { User } from '../../../models/domain/User';
import { ApiUserService } from '../../api/entity-services/user.service';
import { AuthService } from '../../auth/auth.service';
import { AccountService } from '../services/account.service'

@Component({
	selector: 'registration-component',
	templateUrl: 'app/modules/account/templates/registration.component.html'
})
export class RegistrationComponent
{
	public user :User = new User();

	public errorMessage :string;

	public isLoading :boolean;

	constructor
	(
		private userService :ApiUserService,
		private authService :AuthService,
		private accountService :AccountService,
		private router :Router,
		public dialogRef :MatDialogRef<RegistrationComponent>
	)
	{
		this.isLoading = false;
	}

	public onSubmit(e) :void
	{
		e.preventDefault();
		this.isLoading = true;

		this.userService
			.Create(this.user)
			.subscribe(
				user =>
				{
					this.user.id = user.id;
					this.user.login = user.login;


					this.authService
						.Login(this.user)
						.subscribe(
							token =>
							{
								this.accountService.Load()
									.subscribe(
										account =>
										{
											this.user = account;
											this.isLoading = false;
											this.dialogRef.close();

											let redirectRoute = account.activeScopeId
												? `/scope/${account.activeScopeId}`
												: '/scope-list/';

											this.router.navigate([redirectRoute]);
										},
										error => this.errorMessage = <any>error
									);
							},
							error => this.errorMessage = <any>error
						);
				},
				error => this.errorMessage = <any>error
			);
	}
}

最佳答案

将对话框添加到要在许多组件中共享的服务时出现此错误。只是为了澄清,在将对话框移动到服务之前,应用程序中不存在错误。解决方案是包含一个自定义提供程序 MatDialogRef在主模块中

  import { DialogService } from './services/dialog.service';
  import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
  ...
  imports: [
    ...
    MatDialogModule
  ],
  providers: [
     {
       provide: MatDialogRef,
       useValue: {}
     },
     DialogService
  ],
  ...
有了这个提供者,该服务作为一个单例工作,我的对话被共享,提供者错误消失了。

关于angular - NullInjectorError : No provider for MatDialogRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47270324/

相关文章:

javascript - 这个语法是什么,为什么起作用?

javascript - typescript 参数数量错误

Angular 5 Material - 如何将 mat-nav-list anchor 链接设置为在单击时处于事件状态

Angular 2 Material Progress Spinner : display as overlay

angular - 如何从宿主组件 Angular 2 定位组件样式

angular - UrlHelperService 没有提供者

javascript - 删除 |来自 Typescript 类型的 null

javascript - 如何在Angular中按日期字符串(昨天,今天,本月,上个月)过滤

angular - 如何通知父项子组件的更改?

javascript - 如何更改垫子扩展面板的切换图标?