每个站点都有Cookie Consent弹出窗口

时间:2017-11-27 11:52:23

标签: angular typescript cookies

我尝试使用此组件https://github.com/tinesoft/ngx-cookieconsent实施Cookie同意。信息会显示在网站上,但即使您同意它也会在刷新后再次显示。我必须填写这些事件,但我不知道如何。

import { Component, OnInit, OnDestroy } from '@angular/core';
import { NgcCookieConsentService } from 'ngx-cookieconsent';
import { Subscription }   from 'rxjs/Subscription';

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

  //keep refs to subscriptions to be able to unsubscribe later
  private popupOpenSubscription: Subscription;
  private popupCloseSubscription: Subscription;
  private initializeSubscription: Subscription;
  private statusChangeSubscription: Subscription;
  private revokeChoiceSubscription: Subscription;

  constructor(private ccService: NgcCookieConsentService){}

  ngOnInit() {
    // subscribe to cookieconsent observables to react to main events
    this.popupOpenSubscription = this.ccService.popupOpen$.subscribe(
      () => {
        // you can use this.ccService.getConfig() to do stuff...
      });

    this.popupCloseSubscription = this.ccService.popupClose$.subscribe(
      () => {
        // you can use this.ccService.getConfig() to do stuff...
      });

    this.initializeSubscription = this.ccService.initialize$.subscribe(
      (event: NgcInitializeEvent) => {
        // you can use this.ccService.getConfig() to do stuff...
      });

    this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
      (event: NgcStatusChangeEvent) => {
        // you can use this.ccService.getConfig() to do stuff...
      });

    this.revokeChoiceSubscription = this.ccService.revokeChoice$.subscribe(
      () => {
        // you can use this.ccService.getConfig() to do stuff...
      });
  }

  ngOnDestroy() {
    // unsubscribe to cookieconsent observables to prevent memory leaks
    this.popupOpenSubscription.unsubscribe();
    this.popupCloseSubscription.unsubscribe();
    this.initializeSubscription.unsubscribe();
    this.statusChangeSubscription.unsubscribe();
    this.revokeChoiceSubscription.unsubscribe();
  }
}

1 个答案:

答案 0 :(得分:0)

我可以从导入中看到您没有指定配置...

import { NgcCookieConsentService } from 'ngx-cookieconsent';

您需要提取配置并指定一个域以使Cookie正常工作(因此不再纠缠于人们!)

import {NgcCookieConsentModule, NgcCookieConsentConfig} from 'ngx-cookieconsent';

您可以在此处指定域名:

const cookieConfig: NgcCookieConsentConfig = {
  cookie: {
    domain: 'example.com'
  },
  // ...

您在获得NgcCookieConsentModule的地方传递配置,即

NgcCookieConsentModule.forRoot(cookieConfig)
相关问题