python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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
@NgModule({
  //Static, this is the compiler configuration
  //declarations is used for configure the selectors.
  declarations: [
    AppComponent
],
  //Composability and Grouping
  //imports used for composing NgModules together.
  imports: [
    BrowserModule
  ], 
  //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'

@Component({
  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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions