我找到了这篇文章:https://github.com/angular/universal/issues/272,其中user @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'.)\n/home/master/Documents/rs-dash/src/app/app.node.module.ts (97,61)类型上不存在:无法找到名称‘YourServiceWithState’)\n在(/home/master/Documents/universal-cli/packages/@ngtools/webpack/src/loader.ts:115:15)\n at /home/master/Documents/universal-cli/packages/@ngtools/webpack/src/loader.ts:140:17");_checkDiagnostics^
我添加了节点模块:
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
发布于 2017-04-15 12:21:29
我有这个问题,并为它开发了一个缓存&服务器端解决方案(用于服务器和浏览器平台上的缓存操作的4个单独的包)。
检查ng-种子/通用的实现。所有东西都是通过方法/加载器/etc上的@Cached(...)装饰器来处理的,您希望它被缓存。此外,还可以使用缓存API使用缓存方法(has、get、set)。
以下是您需要使用的用于服务器端缓存的包:
https://stackoverflow.com/questions/43350949
复制相似问题