What is the equivalent to AngularJS's ngcookie in Angular 6?

Akshay Sharma picture Akshay Sharma · Jun 9, 2018 · Viewed 77.1k times · Source

How can you create cookies in Angular 6? For AngularJS there was ngcookie. What is the equivalent way to create cookies in Angular 6?

Answer

M.Laida picture M.Laida · Jun 28, 2018
  1. npm install ngx-cookie-service --save
  2. Add to your module: import { CookieService } from 'ngx-cookie-service';
  3. Add CookieService to module's providers.
  4. Inject it into your constructor.
  5. Use cookie.get(nameOfCookie) for getting a specific cookie, use cookie.set(nameOfCookie,cookieValue) for adding a new cookie.