gpt4 book ai didi

angular2-jwt : useFactory directive being ignored

转载 作者:太空狗 更新时间:2023-10-29 18:32:10 26 4
gpt4 key购买 nike

我正在努力集成 angular2-jwt 包。我用自定义配置选项编写了以下函数

import { Http, Headers, RequestOptions } from '@angular/http';
import { provideAuth, AuthHttp, AuthConfig } from 'angular2-jwt';
import { Injectable } from '@angular/core';

export function authHttpServiceFactory(http: Http, options: RequestOptions) {

return new AuthHttp( new AuthConfig({
noTokenScheme: true,
tokenGetter: (() => JSON.parse( localStorage.getItem('access_token') ) ),
globalHeaders: [{'Accept': 'application/json'}],
}), this.http, this.options);
}

在我的模块定义中,我包含了这些行

providers: [
...,
{
provide: AuthHttp,
useFactory: authHttpServiceFactory,
deps: [Http, RequestOptions]
}
],

但是当我尝试在服务中使用 AuthHttp 时,它似乎忽略了我的工厂方法并返回默认实现:

从'angular2-jwt'导入{AuthHttp};

@可注入(inject)()导出类 SsoService {

constructor (
private authHttp: AuthHttp,
) {}

我的模块配置是否正确?我错过了一些简单的事情吗

最佳答案

对于其他人,我将模块更改为使用内联解决方案而不是工厂类,如下所示:

  providers: [
...
AuthHttp,
provideAuth({
noTokenScheme: true,
tokenGetter: (() => JSON.parse( localStorage.getItem('access_token') ) ),
globalHeaders: [{'Accept': 'application/json'}],
})
],

关于angular2-jwt : useFactory directive being ignored,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41570593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com