How to set a cookie in Angular Universal?

时间:2017-12-18 08:07:22

标签: angular cookies angular-universal

In our Angular 4.x project we use Angular Universal in combination with angular2-cookie-law. I am aware of the fact that Angular Universal does not work with $this->load->model('test'); //Model loaded $this->test->create(); // Insertion happened to multiple tables , window etc, but I've read about some work-arounds. I tried the following:

document

And wrap my cookie-law component in the view like this:

if (isPlatformBrowser(this.platformId)) {
    this.showCookieLaw = true;
}

However, this did not work and so far haven't found a different approach. Anyone can guide me the right direction?

1 个答案:

答案 0 :(得分:0)

如果模块使用单独的提供程序作为document.cookie的抽象,则可以为服务器单独定义。但是,它直接使用document全局,因此应该在服务器模块中模拟负责该提供程序的提供程序。

考虑到the component uses service's seen public method,这应该是存根的,例如:

import { CookieLawService } from 'angular2-cookie-law';

exports class MockedCookieLawService {
  seen = () => false;
}

...
imports: [CookieLawModule, ...],
providers: [{ provide: CookieLawService, useClass: MockedCookieLawService }, ...]
...