У меня есть специальный обработчик исключений, который должен выводить пользователя на страницу пользовательской ошибки, если возникает какое-либо исключение (просто попробуйте его).Угловая 2 Пользовательская обработка ошибок и маршрутизатор
Я пытаюсь получить экземпляр маршрутизатора с помощью 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"]);
}
}
Ответа - испытания в 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?) => {
if (!injector) {
return appInjectorRef;
}
appInjectorRef = injector;
return appInjectorRef;
};
2. Added to the bootstrap in the main.ts
bootstrap(AppComponent,[ROUTER_PROVIDERS, HTTP_PROVIDERS,provide(ExceptionHandler,{useClass : AppExceptionHandler})])
.then((appRef) => appInjector(appRef.injector));
3. In the AppExceptionHandler, retrieved the Router instance as shown below
export class AppExceptionHandler {
call(exception:any, stackTrace?:any, reason?:string):void {
let injectorApp = appInjector();
let router = injectorApp.get(Router);
let localStorageService = injectorApp.get(LocalStorageService);
if(exception.message === '-1'){
localStorageService.clear();
router.navigate(["Login"]);
}
}
}
ли вы найти решение этой проблемы? – Scipion
привет обновленный вопрос с ответом. :) – ssujan728