[英]How to store token on server side [Angular Universal]
我发现这篇文章: 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'类型中不存在属性'injector'。)\\ n / home / master / Documents / rs-dash / src / app / app.node.module.ts( 97,61):找不到名称'YourServiceWithState'。)\\ _在_checkDiagnostics(/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" ); ^
我在节点模块中添加了:
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
?
我遇到了这个问题并为它开发了一个缓存和服务器端解决方案(4个独立的包用于服务器和浏览器平台上的缓存操作)。
检查ng-seed / universal的实现 。 一切都是通过方法/ loader / etc上的@Cached(...)
装饰器来处理的,你希望它被缓存。 此外,还可以使用缓存API来使用缓存方法( has
, get
, set
)。
以下是您需要使用的软件包,用于服务器端缓存:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.