您可以定义 aString Token并通过Injectorfrom将其注入到您的组件中@angular/core。
应用模块.ts
const testVar = 'some value';
@NgModule({
...
providers: [
{ provide: 'A', useValue: testVar} // define a string token
]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
成分
import { Injector } from '@angular/core';
constructor(private injector: Injector) {
const A = this.injector.get('A');
console.log(A);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6027 次 |
| 最近记录: |