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()方法时,错误处理服务将显示一个带有错误消息的模态框。

相关内容

热门资讯

攻略讲解!pokemmo手机版... 攻略讲解!pokemmo手机版脚本,hhpoker透视脚本视频,解密教程(有挂软件)是一款可以让一直...
分享实测!wepoker有什么... 分享实测!wepoker有什么规律,hardrock作弊,可靠教程(有挂软件)是由北京得wepoke...
交流学习经验!wepoker底... 交流学习经验!wepoker底牌透视,wepoker网页版透视方法,存在挂教程(有挂教程)关于wep...
发现玩家!德普之星辅助器,we... 1、发现玩家!德普之星辅助器,wepoker手机插件,曝光教程(有挂技巧);详细教程。2、德普之星辅...
一起来讨论!德州局透视脚本免费... 一起来讨论!德州局透视脚本免费版下载手机版,wepoker私人局俱乐部,AA德州教程(有挂技巧);致...
技术分享!we poker辅助... 技术分享!we poker辅助器v3.3,aapoker ai插件,详细教程(有挂攻略);亲真的是有...
实测必看!wepoker智能辅... 实测必看!wepoker智能辅助插件,wepoker轻量版透视系统,我来教教你(有挂神器);原来确实...
玩家必看!wepoker私人局... 玩家必看!wepoker私人局外卦,wepoker怎么设置透视,可靠技巧(有挂攻略)是一款可以让一直...
实测发现!wepoker免费透... 实测发现!wepoker免费透视,pokemomo辅助软件,攻略教程(有挂软件)1、点击下载安装,微...
让我来分享经验!wepoker... 让我来分享经验!wepoker智能辅助插件,wepoker辅助透视,曝光教程(有挂方法);《WPK辅...