如何在Angular Universal中设置cookie?

问题描述 投票:0回答:1

在我们的Angular 4.x项目中,我们将Angular Universal与angular2-cookie-law结合使用。我知道Angular Universal不能与windowdocument等合作,但我已经阅读了一些解决方法。我尝试了以下方法:

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

并在视图中包装我的cookie-law组件,如下所示:

<cookie-law *ngIf="showCookieLaw">...</cookie-law>

然而,这没有用,到目前为止还没有找到不同的方法。任何人都可以指导我正确的方向吗?

angular cookies angular-universal
1个回答
0
投票

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

考虑到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 }, ...]
...
© www.soinside.com 2019 - 2024. All rights reserved.