>
npm install ngx-cookie-monstersbash
npm install ngx-cookie-monsters --save
`
$3
CookieModule should be registered in the AppModule with forRoot() static method and with forChild() in the child modules.\
These methods accepts CookieOptions objects as well. Leave it blank for the defaults.
`typescript
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CookieModule } from 'ngx-cookie-monster';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, CookieModule.forRoot() ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
`
`typescript
import { Component } from '@angular/core';
import { NgxCookieMonsterService } from 'ngx-cookie-monster';
@Component({
selector: 'nom-nom-nom',
template: 'come to the dark side, we have cookies
'
})
export class AppComponent {
constructor(private cookieService: NgxCookieMonsterService){}
getCookie(key: string) {
return this.cookieService.get(key);
}
}
`
CookieService
$3
Returns the value of given cookie key.
`typescript
/**
* @param key ID for lookup.
* @returns Raw cookie value as string.
*/
get(key: string): string;
`
$3
Returns a deserialized Object of given cookie.
`typescript
/**
* @param key Id to use for lookup.
* @returns deserialized cookied value.
*/
getObject(key: string): Object;
`
$3
Returns a key value object with all cookies
`typescript
/**
* @returns All cookies
*/
getAll(): any;
`
$3
Evaluates if cookie exists
`typescript
/**
* @param key ID for lookup
* @returns cookie existance
*/
exists(key: string): boolean;
`
$3
Create a cookie for the given key
`typescript
/**
* @param key ID
* @param value stored raw.
* @param (Optional) Options object.
*/
create(key: string, value: string, options?: CookieOptions): void;
`
$3
Create a cookie with an object of values
`typescript
/**
* @param key ID
* @param value stored serialized
* @param (Optional) Options object.
*/
createFromObject(key: string, value: Object, options?: CookieOptions): void;
`
$3
Removes specific cookie
`typescript
/**
* @param key ID for lookup
*/
remove(key: string): void;
`
$3
Removes all cookies.
`typescript
/**
*/
removeAll(): void;
`
Options
Options object should be a type of CookieOptions interface. The object may have following properties:
- domain - {string} - The cookie will be available only for this domain and
its sub-domains. For security reasons the user agent will not accept the cookie
- path - {string} - The cookie will be available only for this path and its
sub-paths. By default, this is the URL that appears in your tag.
if the current domain is not a sub-domain of this domain or equal to it.
- expires - {string | number | Date} - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT",
number of the form milliseconds or minutes or hours
or a Date object indicating the exact date/time this cookie will expire.
- secure - {boolean} - If true, then the cookie will only be available through a
secured connection.
- httpOnly - {boolean} - If true, then the cookie will be set with the HttpOnly
flag, and will only be accessible from the remote server. Helps to prevent against
XSS attacks.
- storeUnencoded - {boolean} - If true, then the cookie value will not be encoded and
will be stored as provided.
- sameSite - {'none' | 'lax' | 'strict'} - If strict, then it will prevent the cookie
from being sent by the brwoser to the target site in all cross-site browsing context.
If lax`, then it will provide a reasonable balance between security and usabillity for websites