@uniprank/ngx-cookie-service
v1.1.0
Published
Simple library to deal with cookies in Angular.
Downloads
193
Maintainers
Readme
This is an copy of an existing project because the other version doesn't fit with JEST and other testing setups.
See Examples here Example
Installation
First you need to install the npm module:
npm install @uniprank/ngx-cookie-service --save
If you use SystemJS to load your files, you might have to update your config with this if you don't use defaultJSExtensions: true
:
System.config({
packages: {
'@uniprank/ngx-cookie-service': { defaultExtension: 'js' }
}
});
Finally, you can use ngx-cookie-service in your Angular project (NOT AngularJS).
It is recommended to instantiate CookieService
in the bootstrap of your application and to never add it to the "providers" property of your components, this way you will keep it as a singleton.
If you add it to the "providers" property of a component it will instantiate a new instance of the service that won't be initialized.
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { NgxCookieServiceModule } from '@uniprank/ngx-cookie-service';
@NgModule({
imports: [BrowserModule, NgxCookieServiceModule.forRoot()],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
Using
Use Cookie Service
Use CookieService
to set, get or delete cookies.
import { NgModule, Component, OnInit } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CookieService } from '@uniprank/ngx-cookie-service';
@Component({
selector: 'app',
template: `<div></div>`
})
export class AppComponent implements OnInit {
constructor(private _cookieService: CookieService) {}
ngOnInit() {
// check if cookie exists (example)
this._cookieService.check('cookie-name');
// get a cookie (example)
this._cookieService.get('cookie-name');
// get all cookies (example)
this._cookieService.getAll();
// set a cookie (example)
this._cookieService.set('cookie-name', 'cookie-value');
this._cookieService.set('cookie-name', 'cookie-value', 'days' | Date);
this._cookieService.set('cookie-name', 'cookie-value', 'days' | Date, 'path');
this._cookieService.set('cookie-name', 'cookie-value', 'days' | Date, 'path', 'domain');
this._cookieService.set('cookie-name', 'cookie-value', 'days' | Date, 'path', 'domain', 'secure');
this._cookieService.set('cookie-name', 'cookie-value', 'days' | Date, 'path', 'domain', 'secure', 'Lax' | 'Strict');
// delete a cookie
this._cookieService.delete('cookie-name');
this._cookieService.delete('cookie-name', 'path');
this._cookieService.delete('cookie-name', 'path', 'domain');
// delete all cookies
this._cookieService.deleteAll();
this._cookieService.deleteAll('path');
this._cookieService.deleteAll('path', 'domain');
}
}
TODO:
- Finish unit tests