【问题标题】:How to make Angular application read cookies at start up如何让 Angular 应用程序在启动时读取 cookie
【发布时间】:2020-03-15 21:58:09
【问题描述】:

我有一个Angular 应用程序,它在启动时应该读取Cookies (app.component.ts) 并且应该重定向到传递读取值的新组件。我该怎么做?

目前,我的服务器将所需的值作为 html 标记属性提供给<app-root>(例如<app-root something="someValue">),AppComponent 使用this.el.nativeElement.getAttribute("somethings") 读取属性。虽然它有效,但我更喜欢从CookiesHTTP Header 读取值

有没有办法做到这一点?

【问题讨论】:

    标签: angular angular6


    【解决方案1】:

    我很乐意接受其他答案,但迄今为止的研究表明我正在尝试做的事情无法完成。引用来自 (Accessing the web page's HTTP Headers in JavaScript)、Unfortunately, there isn't an API to give you the HTTP response headers for your initial page request. 的回复。所以我目前正在做的可能是可能的解决方法之一 1)我可以设置html属性。如果我担心安全,我可以让他们hidden - https://www.w3schools.com/tags/att_global_hidden.asp 2) 我可以在响应中设置 cookie 并使用纯 javascript (document.cookies) 获取 cookie。 https://www.w3schools.com/js/js_cookies.aspWhat is the equivalent to AngularJS's ngcookie in Angular 6?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多