我已经使用我的应用程序实现了auth,并且在我的代码中使用了自定义AuthHttp。但是,当我运行我的应用程序时,我有这个错误
this._http.request不是函数
我没有安装模块。我只是想使用AuthHttp。所以,我只在我的应用程序中直接导入了AuthHttp,AuthModule,IAuthConfig和AuthConfig 在我的appModule中,我导入了AuthModule
imports: [
...,
AuthModule.forRoot(),
...
]
AuthHttp:
constructor(options: AuthConfig, private _http: Http, private _options: RequestOptions) {
this.config = options.getConfig();
}
private mergeOptions(providedOpts: RequestOptionsArgs, defaultOpts?: RequestOptions) {
let newOptions = defaultOpts || new RequestOptions();
if (this.config.globalHeaders) {
this.setGlobalHeaders(this.config.globalHeaders, providedOpts);
}
newOptions = newOptions.merge(new RequestOptions(providedOpts));
return newOptions;
}
private requestHelper(requestArgs: RequestOptionsArgs, additionalOptions?: RequestOptionsArgs): Observable<Response> {
let options = new RequestOptions(requestArgs);
if (additionalOptions) {
options = options.merge(additionalOptions);
}
return this.request(new Request(this.mergeOptions(options, this._options)));
}
public setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs) {
if (!request.headers) {
request.headers = new Headers();
}
headers.forEach((header: Object) => {
let key: string = Object.keys(header)[0];
let headerValue: string = (header as any)[key];
(request.headers as Headers).set(key, headerValue);
});
}
public request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
if (typeof url === 'string') {
return this.get(url, options); // Recursion: transform url from String to Request
}
// from this point url is always an instance of Request;
let req: Request = url as Request;
let token: string = localStorage.getItem('auth_token');
return this.requestWithToken(req, token);
}
private requestWithToken(req: Request, token: string): Observable<Response> {
req.headers.set(this.config.headerName, this.config.headerPrefix + token);
return this._http.request(req);
}
我像这样使用authHttp
`@Injectable() 导出类UserService {
constructor(
private _authHttp: AuthHttp,
private authenticationService: AuthenticationService) {
}
getUsers(): Observable<User[]> {
// get users from api
return this._authHttp.get('/api/users')
.map((response: Response) => response.json());
}}
此处发生错误(在AuthHttp中)
private requestWithToken(req: Request, token: string): Observable<Response> {
req.headers.set(this.config.headerName, this.config.headerPrefix + token);
return this._http.request(req);
}
AuthModule:
export class AuthModule {
static forRoot(config?: IAuthConfig): ModuleWithProviders {
return {
ngModule: AuthModule,
providers: [
{
provide: AuthHttp,
useFactory: provideAuthHttp,
deps: [
Http,
RequestOptions
]
}
]
}
}}
export function provideAuthHttp(config: IAuthConfig, http: Http, options: RequestOptions) {
return new AuthHttp(new AuthConfig(config), http, options);}
有什么想法吗?