简体   繁体   English

如何在服务器端Angular Universal上获取Cookie

[英]How to get cookie on the server side Angular Universal

I have an Angular 4 app with server side rendering. 我有一个带有服务器端渲染的Angular 4应用程序。 On server side I have such code: 在服务器端,我有这样的代码:

const res: express.Response = this.injector.get('RESPONSE');
res.cookie('Name', 'Value');

So, how in the component can I access this cookies? 那么,如何在组件中访问此cookie? I inject Express RESPONSE in my component, but there are no cookies. 我在组件中注入了Express RESPONSE,但是没有cookie。

@Component({
  selector: 'app-my',
  templateUrl: './my.component.html',
  styleUrls: ['./my.component.scss']
})
export class MyComponent implements OnInit {

  constructor(@Inject(PLATFORM_ID) private platformId: Object, private injector: Injector) {
  }
  ngOnInit() {
    if (isPlatformServer(this.platformId)) {
      let CircularJSON = require('circular-json');
      let res: express.Response = this.injector.get('RESPONSE');
      console.log("RESPONSE = " + CircularJSON.stringify(res));
    }
  }
}

The problem is to get cookie on server side, before they will be set in browser. 问题是要在服务器端获取cookie,然后才能在浏览器中设置它们。

您是否尝试过使用此ngx-cookie-service库访问cookie?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM