[英]Injection token undefined in service, but found in component
I'm adding external js
libraries as InjectionTokens
to an Angular 7 application, so as to use them in @Components
and services. 我正在将外部js
库作为InjectionTokens
添加到Angular 7应用程序中,以便在@Components
和服务中使用它们。 I've created the interface, the InjectionToken<>
and the Provider
, and added the Provider
to the providers: []
property of an @NgModule
. 我已经创建了接口, InjectionToken<>
和Provider
,并将Provider
添加到了@NgModule
的providers: []
属性中。
I've been following this guide on how to inject 3rd party libraries. 我一直在关注这个指南就如何注入第三方库。
When I inject the InjectionToken
in a component, I can use it perfectly well. 当我将InjectionToken
组件时,可以很好地使用它。 When I try to inject it into a service, I get a ... is not defined
error. 当我尝试将其注入服务时,出现... is not defined
错误。
angular.json
: angular.json
:
[...]
"scripts": [
"./node_modules/hot-formula-parser/dist/formula-parser.js",
"./node_modules/@handsontable/formulajs/dist/formula.js"
]
[...]
entry.module.ts
: entry.module.ts
:
import { NgModule, InjectionToken } from '@angular/core';
import { CommonModule } from '@angular/common';
import { EntryComponent } from './entry.component';
import { RouterModule } from '@angular/router';
import { entryRoutes } from './entry.routes';
import { SharedModule } from 'src/shared/shared.module';
import { EntryService } from './entry.service';
import { HotParserProvider} from './parser/parser.injector';
@NgModule({
declarations: [EntryComponent],
imports: [SharedModule, CommonModule, RouterModule.forChild(entryRoutes)],
exports: [],
providers: [
HotParserProvider,
EntryService,
],
})
export class EntryModule { }
parser.injector.ts
: parser.injector.ts
:
import { InjectionToken, ValueProvider } from "@angular/core";
interface IHotParser {
parse: (formula: string) => {error:string, result: any};
}
const HotParser: InjectionToken<IHotParser> = new InjectionToken<IHotParser>('HotParser');
const HotParserProvider: ValueProvider = {
provide: HotParser,
useValue: new window['formulaParser'].Parser()
}
export {IHotParser, HotParser, HotParserProvider};
entry.component.ts
: entry.component.ts
:
import { Component, Inject } from '@angular/core';
import { EntryService } from './entry.service';
import { HotParser, IHotParser } from './parser/parser.injector';
@Component({
selector: 'tp-entry',
templateUrl: './entry.component.html',
styleUrls: ['./entry.component.css']
})
export class EntryComponent {
constructor(
@Inject(HotParser) private parser: IHotParser, // No error!
private entryService:EntryService
) { }
ngOnInit(): void {
console.log(parser.parse("SUM(1,1)"); // Correct output!
}
}
entry.service.ts
: entry.service.ts
:
import { Injectable, Inject } from '@angular/core';
import { IHotParser} from './parser/parser.injector';
@Injectable()
export class EntryService {
constructor(
@Inject(HotParser) private parser: IHotParser // HotParser is not defined
) { }
Is there a difference between injecting a token in a component and in a service? 在组件和服务中注入令牌之间有区别吗? I've tried changing the module provider-array's order, to no avail... 我试图将模块提供者数组的顺序更改为无效...
Nevermind. 没关系。 Visual Studio Code 's linting didn't warn me that HotParser
hadn't been imported in the service's import
statement... I saw the following compiler error: Visual Studio Code的更新没有警告我没有在服务的import
语句中导入HotParser
...我看到了以下编译器错误:
ERROR in src/entry/entry.service.ts(11,25): error TS2304: Cannot find name 'HotParser'.
So ended up changing: 所以最终改变了:
import { IHotParser } from './parser/parser.injector';
to: 至:
import { HotParser, IHotParser } from './parser/parser.injector';
And the error went away. 错误消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.