我们有一个Angular Universal应用程序,需要在服务器端运行时从node.js传递一个值到angular。我们通过在server.ts中使用以下代码来解决这个问题:
在组件中,我们使用以下代码解析此对象:
const theValue: TheType = nodeLogicToRetrieveValue();
app.engine('html', (_, options, callback) => {
let engine = ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP),
{ provide: 'theKey', useFactory: () => theValue, deps: [] }
]
});
engine(_, <any>options, callback)
});
在组件中,我们使用以下代码解析此对象:
if (isPlatformServer(this.platformId)) {
this.theValue = this.injector.get('theKey');
}
这个代码可以正常工作,但是代码检查会给出以下警告:
get已经过时:从v4.0.0开始使用Type<T>或InjectionToken<T>
我们尝试将其更改为使用InjectionToken (new InjectionToken<TheType>('theKey')
)或类型而不是字符串,但这样无法正常工作:
错误:StaticInjectorError(AppServerModule)[TheType]: StaticInjectorError(Platform: core)[TheType]: NullInjectorError:TheType没有提供者!
是否只能通过字符串令牌从node传递值到angular中?