Ionic2混合开发,入坑系列:Ionic2中腾讯Bugly异常捕获以及上报
1、Ionic2中处理全局异常,直接继承IonicErrorHandler即可,代码如下
import { IonicErrorHandler } from 'ionic-angular'; import { Bugly } from './../plugins/TX-bugly.plugin' import { Config } from './../config/config' /** * Ionic2全局异常捕获 */ export class GlobalIonicErrorHandler extends IonicErrorHandler { /** * 异常处理 * @param err 异常信息 */ handleError(err: any): void { let _message = err._nativeError || err.message || err._nativeError.message || ""; let _stack = err._nativeError || err.stack || err._nativeError.stack; let params: any = { message: _message, stack: _stack, platform: Config.APP_PLATFORM }; console.log("错误:" + _message); console.log("堆栈:" + _stack); Bugly.buglySend(JSON.stringify(params)); } }
2、在AppModule.ts中将自定义的异常扩类添加到providers中
import { NgModule, ErrorHandler } from '@angular/core'; import { IonicApp, IonicModule } from 'ionic-angular'; import { MyApp } from './app.component'; import './rxjs-extensions'; import { GlobalIonicErrorHandler } from './global-error-handler-ionic.extend'
@NgModule({ declarations: [ MyApp ], bootstrap: [IonicApp], entryComponents: [ MyApp ], imports: [ IonicModule.forRoot(MyApp, { tabsHideOnSubPages: true, iconMode: 'ios', modalEnter: 'modal-slide-in', modalLeave: 'modal-slide-out', tabsPlacement: 'bottom', pageTransition: 'ios' }), ComponentBusinessModule ], providers: [ { provide: ErrorHandler, useClass: GlobalIonicErrorHandler }] }) export class AppModule { }