我有一个自定义异常处理程序,如果发生任何异常(只是尝试它),它应该将用户带到自定义错误页面.
我试图使用Injector获取路由器的实例.这样做的原因,我相信注入器将给现有的路由器实例并使用它我将能够路由用户.
任何想法为什么这不起作用或如何实现这一点?
谢谢 :)
@Injectable()
export class AppExceptionHandler extends ExceptionHandler{
constructor(){
super(null, null);
}
call(exception:any, stackTrace?:any, reason?:string):void {
console.log('call...')
var providers = Injector.resolve([ROUTER_PROVIDERS]);
var injector = Injector.fromResolvedProviders(providers);
// this is causing issue, not sure it is the correct way
let router : Router = injector.get(Router);
// not executed
console.log(router)
// not executed
console.log('done...')
router.navigate(["CustomErrorPage"]);
}
}
Run Code Online (Sandbox Code Playgroud)
答案 - 在2.0.0-beta.17中测试感谢Druxtan
1. Created a file app.injector.ts inside the app folder (app/app.injector.ts)
let appInjectorRef;
export const appInjector = (injector?) => { …Run Code Online (Sandbox Code Playgroud) angular ×1