gpt4 book ai didi

angular - 空注入(inject)器错误 : No provider for MatDialogRef

转载 作者:太空狗 更新时间:2023-10-29 16:53:35 28 4
gpt4 key购买 nike

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

当我尝试这样做时出现错误:

错误错误:StaticInjectorError [MatDialogRef]:StaticInjectorError [MatDialogRef]:NullInjectorError:没有 MatDialogRef 的提供者!

应用程序模块.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 {}

home.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 - 空注入(inject)器错误 : No provider for MatDialogRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47270324/

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