我正在使用自定义Http提供程序来处理API身份验证错误.在我的CustomHttp中,当API发出401状态错误时,我需要将用户重定向到登录页面.这很好用!
app.module.ts
export function loadCustomHttp(backend: XHRBackend, defaultOptions: AppRequestOptions,
router: Router, dataHelper: DataHelperService) {
return new CustomHttp(backend, defaultOptions, router, dataHelper);
}
@NgModule({
// some declarations, imports, ...
providers: [
// some services ...
{
provide: Http,
useFactory: loadCustomHttp,
deps: [XHRBackend, RequestOptions, Router, DataHelperService]
}
});
Run Code Online (Sandbox Code Playgroud)
定制http.ts
import { Injectable } from '@angular/core';
import { Http, RequestOptions, RequestOptionsArgs, ConnectionBackend, Request, Response } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { DataHelperService } from '../helpers/data-helper.service'; …Run Code Online (Sandbox Code Playgroud) 我已将此代码添加到我的app.module.ts中
providers: [ AppConfigService,
{
provide: APP_INITIALIZER,
useFactory: (config: AppConfigService) => () => config.getAppConfig(),
deps: [AppConfigService],
multi: true
},
]
Run Code Online (Sandbox Code Playgroud)
得到这个错误
问题:我已经使用ngx-permission pkg获得许可.我设置了路由权限方法.但是当我刷新页面时,主页上的时间重定向却停留在当前页面上.所以我试图在应用程序启动方法之前加载权限来解决此问题但是出现了此错误.
config.getAppConfig() // call when application start up
Run Code Online (Sandbox Code Playgroud)
有任何想法请帮忙.其他解决方案也欢迎.
我正在尝试检索APP_INITIALIZER中网址中存在的数据
app.module.ts
export function init(config: ConfigService, router: Router) {
return () => config.load(router);
}
providers : [
...
{
provide: APP_INITIALIZER,
useFactory: init,
deps: [ConfigService, Router],
multi: true
},
ConfigService
...
]
Run Code Online (Sandbox Code Playgroud)
config-service.ts
@Injectable()
export class ConfigService
load(router: Router): Promise<any> {
console.log('current url : ' + router.url);
return new Promise(((resolve, reject) => resolve()));
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我越来越
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppBrowserModule in ./AppBrowserModule@-1:-1
Run Code Online (Sandbox Code Playgroud)
我也尝试Injector在构造函数中使用,但是它也不起作用。
我正在尝试做的事可行吗?