gpt4 book ai didi

javascript - 如何通过 Angular-Service 显示引导模式

转载 作者:行者123 更新时间:2023-12-02 22:39:37 24 4
gpt4 key购买 nike

我有一个全局错误处理程序,用于处理客户端和服务器错误。

为了向用户提供反馈,我想打开一个返回错误消息的模式。

因此我实现了一个模式:

import {Component} from '@angular/core';
import {BsModalRef, BsModalService} from 'ngx-bootstrap';
import {Button} from '../../layout-models/button.model';

@Component({
selector: 'error-modal',
templateUrl: './error-modal.component.html',
styleUrls: ['./error-modal.component.scss']
})
export class ErrorModalComponent {
title: string;
buttonTitle = 'OK';
type: 'error';
button: Button;

protected modalRef: BsModalRef;

constructor(protected modalService: BsModalService) {}

public show(title: string, message: string) {
this.title = title;
this.modalRef = this.modalService.show(
message,
Object.assign({}, { class: `modal-banner ${this.type}`})
);
}

hide() {
if (this.modalRef) {
this.modalRef.hide();
}
}
}

在我的通知服务中:

import {Injectable, NgZone} from '@angular/core';
import { ErrorModalComponent } from '../error-modal.component';

@Injectable({
providedIn: 'root'
})
export class NotificationService {

public errorModalComponent: ErrorModalComponent;

showError(title: string, message: string): void {
this.errorModalComponent.show(title, message);
}
}

这导致
未捕获的类型错误:无法读取未定义的属性“show”

我觉得我犯了一些根本性的错误 - 这样做的主要目的是拥有一个集中模式。这是可能的还是我需要在每个想要显示错误处理模态的组件中使用 ModalComponent ?

最佳答案

我不会使用 ngx-modal 我会使用 NgbModal

yazantahhan 的意思是这样的:

import {Injectable} from "@angular/core";
import {NgbModal, NgbModalRef} from "@ng-bootstrap/ng-bootstrap";

@Injectable()
export class ErrorModalService {

title: string;
buttonTitle = "OK";
type: "error";

protected modalRef: NgbModalRef;

constructor(protected modalService: NgbModal) {}

public show(title: string, message: string) {
this.title = title;
this.modalRef = this.modalService.open(
message
);
}

hide() {
if (this.modalRef) {
this.modalRef.close();
}
}
}
然后像这样注入(inject)并使用它:

import { Component } from "@angular/core";
import {ErrorModalService} from "./ErrorModalService";

@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.scss"]
})
export class AppComponent {
title = "testAngular";

constructor(
private errorModalService: ErrorModalService,
) {}


showError() {
this.errorModalService.show("title", "message");
}
}

不要忘记在您的模块中提供服务

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import {ErrorModalService} from "./ErrorModalService";
import {BsModalService} from "ngx-bootstrap/modal";
import {NgbModule} from "@ng-bootstrap/ng-bootstrap";

@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
NgbModule,
],
providers: [
ErrorModalService,
],
bootstrap: [AppComponent],
})
export class AppModule { }

关于javascript - 如何通过 Angular-Service 显示引导模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58627565/

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