gpt4 book ai didi

javascript - 棱 Angular cookies

转载 作者:IT王子 更新时间:2023-10-29 02:53:19 27 4
gpt4 key购买 nike

我一直在四处寻找 Angular cookie,但一直没能找到如何在 Angular 中实现 cookie 管理。有没有什么方法可以管理 cookie(比如 AngularJS 中的 $ cookie)?

最佳答案

我结束了创建自己的函数:

@Component({
selector: 'cookie-consent',
template: cookieconsent_html,
styles: [cookieconsent_css]
})
export class CookieConsent {
private isConsented: boolean = false;

constructor() {
this.isConsented = this.getCookie(COOKIE_CONSENT) === '1';
}

private getCookie(name: string) {
let ca: Array<string> = document.cookie.split(';');
let caLen: number = ca.length;
let cookieName = `${name}=`;
let c: string;

for (let i: number = 0; i < caLen; i += 1) {
c = ca[i].replace(/^\s+/g, '');
if (c.indexOf(cookieName) == 0) {
return c.substring(cookieName.length, c.length);
}
}
return '';
}

private deleteCookie(name) {
this.setCookie(name, '', -1);
}

private setCookie(name: string, value: string, expireDays: number, path: string = '') {
let d:Date = new Date();
d.setTime(d.getTime() + expireDays * 24 * 60 * 60 * 1000);
let expires:string = `expires=${d.toUTCString()}`;
let cpath:string = path ? `; path=${path}` : '';
document.cookie = `${name}=${value}; ${expires}${cpath}`;
}

private consent(isConsent: boolean, e: any) {
if (!isConsent) {
return this.isConsented;
} else if (isConsent) {
this.setCookie(COOKIE_CONSENT, '1', COOKIE_CONSENT_EXPIRE_DAYS);
this.isConsented = true;
e.preventDefault();
}
}
}

关于javascript - 棱 Angular cookies ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34298133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com