how to use CookieStore in Angular in order to listen to cookie change events

Discussão em 'Angular' iniciado por fascynacja, Novembro 19, 2024.

  1. fascynacja

    fascynacja Guest

    I have an angular component where I would like to perform actions when a specific cookie is changed. I read that I can do it via CookieStore Api (https://developer.mozilla.org/en-US/docs/Web/API/CookieStore/change_event) But when I try to use it in my angular component, the code does not compile:

    ngOnInit() {

    if ('cookieStore' in window) {
    // Listen for changes to cookies
    window.cookieStore.addEventListener('change', event => {
    event.changed.forEach(change => {
    console.log(`Cookie changed: ${change.name} = ${change.value}`);
    });
    });
    } else {
    console.log('Cookie Store API is not supported in this browser.');
    }
    }


    Error is

    TS2339: Property cookieStore does not exist on type Window & typeof globalThis


    I am using angular version: 15.2.10. Is there any way I can use the CookieStore Api in my angular code? Either service or component?

    Continue reading...

Compartilhe esta Página