我发现这篇文章:https ://github.com/angular/universal/issues/272其中用户@gdi2290 解释了如何添加该功能
但是我的代码中出现错误:
/home/master/Documents/rs-dash/dist/server/server.bundle.js:317 抛出新错误(“模块构建失败:错误:/home/master/Documents/rs-dash/src/app/app. node.module.ts (97,48):“ApplicationRef”类型上不存在属性“injector”。)\n/home/master/Documents/rs-dash/src/app/app.node.module.ts( 97,61): 在 _checkDiagnostics (/home/master/Documents/universal-cli/packages/@ngtools/webpack/src/loader.ts:115:15) 处找不到名称“YourServiceWithState”。\n 在 / home/master/Documents/universal-cli/packages/@ngtools/webpack/src/loader.ts:140:17"); ^
我在节点模块中添加:
constructor(public appRef: ApplicationRef) {
}
// lives in your node main module
universalDoDehydrate(universalCache) {
var yourServiceWithState = this.appRef.injector.get(YourServiceWithState)
var key = 'yourServiceWithState._state'
universalCache[key] = yourServiceWithState._state.justOneProp
}
在浏览器模块中:
constructor(public appRef: ApplicationRef) {
// lives in your browser main module
var key = 'yourServiceWithState._state'
if (UNIVERSAL_CACHE[key]) {
localStorage[key] = UNIVERSAL_CACHE[key]
}
}
有人知道我正在使用什么问题universal-cli
吗?