Angular 7的SSR(通用)Cookie

8
我正在尝试为我的angular应用程序实现SSR。我遵循了universal starter guide(https://angular.io/guide/universal)。
我的angular应用程序使用rest API,需要一个access_token (client_credentials grant_type)来处理所有请求。我使用cookie在客户端存储它。
我知道cookie不能从头开始在服务器端工作。所以我尝试了一些包(ngx-cookie-service),但我找不到一个适用于SSR angular 7的包...
最近我发现了这个包(ngx-auth),但我不确定我能否将其用于此目的...
我无法在universal网站上找到任何文档,如果有人已经成功地使用angular 7为universal实现了cookie...
非常感谢!
2个回答

7

1
使用 https://github.com/Gorniv/ngx-universal/blob/master/lib/src/cookie/README.md - 从 ngx-cookie 分支并修复了 SSR 的问题。 - gorniv
你可以使用此问题的修复程序,而不是fork:https://github.com/salemdar/ngx-cookie/issues/104#issuecomment-568108352 - Dukeatcoding

0
here's my solution : 

  getssrCockie(){
    if (this.req !== null) {
      const cookies = new Cookies(this.req.headers.cookie);
      const clubInfo = cookies.get('clubInfo')
      return clubInfo
    } else {
      const cookies = new Cookies();
      const clubInfo2 = cookies.get('clubInfo')
      return clubInfo2
    } 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接