7 Cookies

Install cookies in Angular

How to install a cookie in Angular?
Install cookie -

npm install ngx-cookie-service –save

If you do not want to install this via NPM, you can run npm run compile and use the *.d.ts and *.js files in the dist-lib folder.

After installed successfully, add the cookie service in the Angular module - app.module.ts
import {CookieServicefrom 'ngx-cookie-service'

//AppModule class with @NgModule decorator
  //Static, this is the compiler configuration
  //declarations is used for configure the selectors.
  declarations: [
  //Composability and Grouping
  //imports used for composing NgModules together.
  imports: [
  //Runtime or injector configuration
  //providers is used for runtime injector configuration.
  providers: [CookieService],
  //bootstrapped entry component
  bootstrap: [AppComponent]
export class AppModule { }

Then, import and inject it into a component -
import { ComponentOnInit } from '@angular/core';
import {CookieServicefrom 'ngx-cookie-service'

  selector: 'app-on-click',
  templateUrl: './on-click.component.html',
  styleUrls: ['./on-click.component.css']
export class OnClickComponent implements OnInit {

  cookieValue ="";
  constructor(private cookie:CookieService) {  }

  ngOnInit() {
    this.cookie.set('cookie''demoApp' );
    this.cookieValue = this.cookie.get('cookie');


Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Install cookies in Angular Install cookies in Angular Reviewed by Anil Singh on 10:04 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.