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

相关内容

热门资讯

德普辅助器怎么用!wpk德州局... 德普辅助器怎么用!wpk德州局透视(透视)方法-总是了解真的有挂1、德普辅助器怎么用公共底牌简单,德...
透视有挂!wepoker免费钻... 透视有挂!wepoker免费钻石(透视)德扑之心免费透视,教程课程(存在有挂)-哔哩哔哩1、下载好德...
wepoker透视底牌!wep... wepoker透视底牌!wepoker插件功能辅助器(透视)攻略-竟然推荐真的是有挂1、wepoke...
透视有挂!WePoKer辅助器... 透视有挂!WePoKer辅助器(透视)hhpoker脚本下载,教程总结(发现有挂)-哔哩哔哩1、实时...
WePOker有没有透视方法!... WePOker有没有透视方法!hardrock透视工具(透视)教程-原来推荐存在有挂运WePOker...
透视必备!wepoker有没有... 透视必备!wepoker有没有挂(透视)epoker底牌透视,教程模板(有挂功能)-哔哩哔哩该软件可...
透视详情!pokeplus脚本... 透视详情!pokeplus脚本(透视)wepoker辅助透视软件,教程妙招(有挂讲解)-哔哩哔哩1、...
wepoker可以免费玩吗!p... wepoker可以免费玩吗!pokemmo辅助器手机版下载(透视)软件-总是科普真的是有挂1、进入游...
透视解密!德普之星私人局辅助器... 透视解密!德普之星私人局辅助器(透视)we-poker辅助器,教程大纲(有人有挂)-哔哩哔哩1、许多...
hhpoker有后台操控吗!德... hhpoker有后台操控吗!德州局HHpoker透视脚本(透视)神器-本来关于存在有挂1)德州局HH...