javascript - Angular cookies
Hľadal som všade Angular cookies, ale nedokázal som nájsť, ako implementovať spravovanie cookies v Angular. Existuje nejaký spôsob, ako spravovať cookies (ako $cookie v AngularJS)?
Odpoveď č. 1
Nakoniec som vytvoril svoje vlastné funkcie:
@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();
}
}
}
Odpoveď č. 2
Aktualizácia: angular2-cookie je teraz zastaraný. Prosím, použite namiesto toho môj ngx-cookie.
Stará odpoveď: :
Tu je angular2-cookie, čo je presná implementácia služby Angular 1 $cookies (plus metóda removeAll()), ktorú som vytvoril. Používa tie isté metódy, len je implementovaná v TypeScripte s logikou Angular 2.
Môžete ho injektovať ako službu do poľa providers komponentov:
import {CookieService} from 'angular2-cookie/core';
@Component({
selector: 'my-very-cool-app',
template: '<h1>My Angular2 App with Cookies</h1>',
providers: [CookieService]
})
Potom ho definujte v konštruktore ako obvykle a začnite používať:
export class AppComponent {
constructor(private _cookieService:CookieService){}
getCookie(key: string){
return this._cookieService.get(key);
}
}
Môžete to získať cez npm:
npm install angular2-cookie --save
Odpoveď č. 3
Použite NGX Cookie Service
Nainštalujte tento balík: npm install ngx-cookie-service --save
Pridajte službu cookie do vášho súboru app.module.ts ako poskytovateľa:
import { CookieService } from 'ngx-cookie-service';
@NgModule({
declarations: [ AppComponent ],
imports: [ BrowserModule, ... ],
providers: [ CookieService ],
bootstrap: [ AppComponent ]
})
Potom zavolajte svoj komponent:
import { CookieService } from 'ngx-cookie-service';
constructor( private cookieService: CookieService ) { }
ngOnInit(): void {
this.cookieService.set( 'name', 'Test Cookie' ); // To Set Cookie
this.cookieValue = this.cookieService.get('name'); // To Get Cookie
}
To je všetko!
Odpoveď č. 4
Áno, tu je jeden ng2-cookies
Použitie :
import { Cookie } from 'ng2-cookies/ng2-cookies';
Cookie.setCookie('cookieName', 'cookieValue');
Cookie.setCookie('cookieName', 'cookieValue', 10 /*days from now*/);
Cookie.setCookie('cookieName', 'cookieValue', 10, '/myapp/', 'mydomain.com');
let myCookie = Cookie.getCookie('cookieName');
Cookie.deleteCookie('cookieName');
