NullInjectorError: No provider for NzModalRef!

原文链接:https://www.longkui.site/program/frontend/nullinjectorerror-no-provider-for-nzmodalref/4926/

0.背景
调试一个angular项目是出现了路由错误,报错没有找到这个路由。打开F12看报错信息,看到下面这些:

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[TestComponent -> NzModalRef]:
StaticInjectorError(Platform: core)[TestComponent -> NzModalRef]:
NullInjectorError: No provider for NzModalRef!
Error: StaticInjectorError(AppModule)[TestComponent -> NzModalRef]:
StaticInjectorError(Platform: core)[TestComponent -> NzModalRef]:
NullInjectorError: No provider for NzModalRef!

1.原因及解决办法
NzModalRef 是一个动态产生模态框的组件,通过它可以动态的创建Modal。

上面的问题原因的出于下面的代码,

import { Component, OnInit } from ‘@angular/core’;
import {NzModalRef } from “ng-zorro-antd”;
@Component({
selector: ‘test’,
templateUrl: ‘./test.component.html’,
styleUrls: [‘./test.component.less’]
})
export class TestComponent implements OnInit {

constructor(
editModal:NzModalRef
) { }

ngOnInit() {
}

}
解决办法1:

删除 NzModalRef 的导入和 construtor里面有关的内容

解决办法2:

上面editModal:NzModalRef 的部分不对,可以改成下面这样

constructor(

) { }

editModal:NzModalRef
ngOnInit() {
}
这样就正常了,不会报错了。

2.使用方法
正确使用NzModalRef的参考代码:

import {Component, OnInit, TemplateRef} from ‘@angular/core’;
import {NzMessageService, NzModalRef, NzModalService} from “ng-zorro-antd”;
@Component({
selector: ‘test’,
templateUrl: ‘./test.component.html’,
styleUrls: [‘./test.component.less’]
})
export class TestComponent implements OnInit {

constructor(
private modalService: NzModalService,
private message: NzMessageService,
) { }
editModal:NzModalRef
ngOnInit() {
}

openModal(title: TemplateRef<{}>, content: TemplateRef<{}>, footer: TemplateRef<{}>): void {
this.editModal = this.modalService.create({
nzTitle: title,
nzContent: content,
nzFooter: footer,
nzWidth:1200,
nzMaskClosable: false
});
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值