Не удается внедрить InjectionToken в конструктор службы - PullRequest
0 голосов
/ 05 января 2019

Мне нужно решить, почему мой инъекционный токен не внедряется в конструктор класса. У меня есть угловой (7.1), код сгенерированный из NSwag.

Я уже пытался поместить код провайдера в модуль и компонент. Спасибо за совет.

client.ts

export const API_BASE_URL = new InjectionToken<string>('API_BASE_URL');

export class Client implements IClient {
private http: HttpClient;
private baseUrl: string;
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
// Removed @Optional for debug purpose
constructor(@Inject(HttpClient) http: HttpClient, @Inject(API_BASE_URL) baseUrl?: string) {
    this.http = http;
    this.baseUrl = baseUrl ? baseUrl : "";
}
}

app.module.ts

const API_BASE_URL = new InjectionToken<string>('API_BASE_URL');

// providers array
providers: [
    {provide: API_BASE_URL, useValue: 'localhost:1234'},
    Client,
    ArticleService
]

article.service.ts

@Injectable({
  providedIn: 'root'
})
export class ArticleService {

  client: Client;

  constructor(client: Client) {
    this.client = client;
  }
}


ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule) 
[Client -> InjectionToken API_BASE_URL]: 
  StaticInjectorError(Platform: core)[Client -> InjectionToken API_BASE_URL]: 
   NullInjectorError: No provider for InjectionToken API_BASE_URL!
Error: StaticInjectorError(AppModule)[Client -> InjectionToken API_BASE_URL]: 
  StaticInjectorError(Platform: core)[Client -> InjectionToken API_BASE_URL]: 
    NullInjectorError: No provider for InjectionToken API_BASE_URL!
...