Angular 6错误处理 - 如何在模态框中显示错误?
创始人
2024-10-16 06:30:42
0

要在Angular 6的模态框中显示错误,可以按照以下步骤进行操作:

  1. 创建一个错误处理服务(ErrorHandlingService):
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class ErrorHandlingService {
  private errorMessageSubject = new BehaviorSubject(null);
  errorMessage$ = this.errorMessageSubject.asObservable();

  constructor() { }

  displayError(message: string) {
    this.errorMessageSubject.next(message);
  }
}
  1. 在模态框组件中注入错误处理服务:
import { Component, OnInit } from '@angular/core';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { ErrorHandlingService } from 'path-to-error-handling-service';

@Component({
  selector: 'app-modal',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {
  closeResult: string;

  constructor(private modalService: NgbModal, private errorHandlingService: ErrorHandlingService) { }

  ngOnInit() {
    this.errorHandlingService.errorMessage$.subscribe(message => {
      if (message) {
        this.openModal(message);
      }
    });
  }

  open(content) {
    this.modalService.open(content, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
      this.closeResult = `Closed with: ${result}`;
    }, (reason) => {
      this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
    });
  }

  private openModal(message: string) {
    this.modalService.open(ModalContentComponent, { size: 'lg' }).componentInstance.message = message;
  }

  private getDismissReason(reason: any): string {
    if (reason === ModalDismissReasons.ESC) {
      return 'by pressing ESC';
    } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
      return 'by clicking on a backdrop';
    } else {
      return `with: ${reason}`;
    }
  }
}
  1. 创建一个用于显示错误信息的模态框内容组件(ModalContentComponent):
import { Component, Input } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';

@Component({
  selector: 'app-modal-content',
  template: `
    
    
    
  `
})
export class ModalContentComponent {
  @Input() message: string;

  constructor(public activeModal: NgbActiveModal) { }
}
  1. 在需要显示错误的地方,调用错误处理服务的displayError方法:
import { Component } from '@angular/core';
import { ErrorHandlingService } from 'path-to-error-handling-service';

@Component({
  selector: 'app-root',
  template: `
    
    
  `,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private errorHandlingService: ErrorHandlingService) { }

  triggerError() {
    this.errorHandlingService.displayError('Something went wrong');
  }
}

通过以上步骤,当调用triggerError()方法时,错误处理服务将显示一个带有错误消息的模态框。

相关内容

热门资讯

7分钟辅助!粤友圈有挂吗,其实... 7分钟辅助!粤友圈有挂吗,其实有辅助挂(证实有挂)1、每一步都需要思考,不同水平的挑战会更加具有挑战...
四分钟辅助!麻将外卦软件框架模... 四分钟辅助!麻将外卦软件框架模式,切实有辅助攻略(有挂方式)1)麻将外卦软件框架模式有没有挂:进一步...
第九分钟辅助!微乐手游脚本辅助... 第九分钟辅助!微乐手游脚本辅助,一贯是真的有辅助技巧(有挂方针)1、完成微乐手游脚本辅助有辅助插件,...
第二分钟辅助!微乐小程序辅助脚... 第二分钟辅助!微乐小程序辅助脚本,竟然真的是有辅助工具(有挂分享)微乐小程序辅助脚本透视方法中分为三...
8分钟辅助!相约互娱辅助,真是... 8分钟辅助!相约互娱辅助,真是是真的有辅助app(有挂教学)1、玩家可以在相约互娱辅助透视最简单三个...
第三分钟辅助!黑桃k大厅辅助,... 第三分钟辅助!黑桃k大厅辅助,总是有辅助神器(有挂秘笈)1、进入游戏-大厅左侧-新手福利-激活码辅助...
第二分钟辅助!中至上饶打炸漏洞... 第二分钟辅助!中至上饶打炸漏洞,真是是有辅助插件(了解有挂)1、中至上饶打炸漏洞模拟器是什么优化,中...
两分钟辅助!温州茶苑破解版,好... 您好,温州茶苑破解版这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...
1分钟辅助!禅游指尖四川修改器... 1分钟辅助!禅游指尖四川修改器,果然有辅助方法(有挂总结)1、这是跨平台的禅游指尖四川修改器轻量版有...
第一分钟辅助!情怀西游辅助,好... 第一分钟辅助!情怀西游辅助,好像是有辅助教程(有挂方法)1、全新机制【情怀西游辅助ai辅助工具激活码...