我们有一个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');
}
这很好,但是掉毛会发出以下警告:
已弃用:从v4.0.0起使用Type
或InjectionToken
我们尝试将其更改为,以便我们使用InjectionToken(new InjectionToken<TheType>('theKey')
)或类型而不是字符串,但这将不起作用:
错误:StaticInjectorError(AppServerModule)[TheType]: StaticInjectorError(平台:核心)[TheType]: NullInjectorError:TheType没有提供程序!
是否只能通过字符串标记将值从节点传递到角度?