gpt4 book ai didi

angular - Ionic 2 异常处理不起作用?

转载 作者:太空狗 更新时间:2023-10-29 17:52:22 24 4
gpt4 key购买 nike

我正在尝试使用 angular 2 创建一个 ionic-2 混合应用程序。我已经实现了 ErrorHandler 接口(interface),但是当发生错误时,什么也不会发生。我不知道这是否是实现错误处理程序的正确方法这是我在 ionic 2 中的第一个应用程序。

这是我的应用模块代码

import { NgModule, ErrorHandler} from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { Page1 } from '../pages/page1/page1';
import { Page2 } from '../pages/page2/page2';
import { homePage } from '../pages/home/homePage';
import { MyPage } from '../pages/my-page/my-page';
import { FormsPage } from '../pages/forms/forms';
import { SharedData } from '../providers/shared-data';
import {MyExceptionHandler} from '../validators/myhandler';


@NgModule({
declarations: [
MyApp,
Page1,
Page2,
homePage,
MyPage,
FormsPage

],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
Page1,
Page2,
homePage,
MyPage,
FormsPage

],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler},SharedData]
})
export class AppModule {}

我的应用组件

import { NgModule,Component, ViewChild,ErrorHandler} from '@angular/core';
//import {provide} from '@angular2/core';
import { Nav, Platform, } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';

import { Page1 } from '../pages/page1/page1';
import { Page2 } from '../pages/page2/page2';
import{homePage} from '../pages/home/homePage';
import { FormsPage } from '../pages/forms/forms';
import {MyExceptionHandler} from '../validators/myhandler';
@NgModule({
providers: [{provide: ErrorHandler, useClass: MyExceptionHandler}]
})

@Component({
templateUrl: 'app.html'

})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = homePage;
pages: Array<{title: string, component: any}>;

constructor(public platform: Platform) {
this.initializeApp();

// used for an example of ngFor and navigation
this.pages = [
{ title: 'gotohomepage', component: homePage },
{ title: 'gotopage1', component: Page1 },
{ title: 'gotopage2', component: Page2 },
{ title: 'forms', component: FormsPage }
];

}

initializeApp() {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
});
}

openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
}

和我的处理程序页面

import { Component,ViewChild,ErrorHandler } from '@angular/core';

export class MyErrorHandler extends IonicErrorHandler {
handleError(err: any): void {
super.handleError(err);//call parent handleError
}

ionViewDidLoad() {
alert('ionViewDidLoad FormsPage');
}
}

谢谢,

最佳答案

  1. 您需要覆盖自定义类中的handleError 函数。

    handleError(err: any): void {
    super.handleError(err);//调用父 handleError
    //处理错误
    }

  2. 您还将 IonicErrorHandler 设置为您的提供者。检查 herethis is the github link

您可以将提供程序数组设置为:

 providers: [{provide: ErrorHandler, useClass: MyExceptionHandler },SharedData]

或者我建议改为尝试扩展 IonicErrorHandler

import { Component,ViewChild,ErrorHandler } from '@angular/core';

export class MyExceptionHandler extends IonicErrorHandler implements ErrorHandler{
handleError(err: any): void {
super.handleError(err);//call parent handleError
// do something with the error
}
}

关于angular - Ionic 2 异常处理不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41628389/

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