I'm currently working on implementing a cookie consent feature using the component found at https://github.com/tinesoft/ngx-cookieconsent. The information does display on the site, but even after agreeing, it keeps reappearing upon refresh. I understand that I need to handle certain events, but I'm unsure of how to do so.
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 {
//keeping references to subscriptions for later unsubscription
private popupOpenSubscription: Subscription;
private popupCloseSubscription: Subscription;
private initializeSubscription: Subscription;
private statusChangeSubscription: Subscription;
private revokeChoiceSubscription: Subscription;
constructor(private ccService: NgcCookieConsentService){}
ngOnInit() {
// subscribing to cookieconsent observables to react to key events
this.popupOpenSubscription = this.ccService.popupOpen$.subscribe(
() => {
// you can use this.ccService.getConfig() to perform actions...
});
this.popupCloseSubscription = this.ccService.popupClose$.subscribe(
() => {
// you can use this.ccService.getConfig() to perform actions...
});
this.initializeSubscription = this.ccService.initialize$.subscribe(
(event: NgcInitializeEvent) => {
// you can use this.ccService.getConfig() to perform actions...
});
this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
(event: NgcStatusChangeEvent) => {
// you can use this.ccService.getConfig() to perform actions...
});
this.revokeChoiceSubscription = this.ccService.revokeChoice$.subscribe(
() => {
// you can use this.ccService.getConfig() to perform actions...
});
}
ngOnDestroy() {
// unsubscribing from cookieconsent observables to avoid memory leaks
this.popupOpenSubscription.unsubscribe();
this.popupCloseSubscription.unsubscribe();
this.initializeSubscription.unsubscribe();
this.statusChangeSubscription.unsubscribe();
this.revokeChoiceSubscription.unsubscribe();
}
}