ng service (service)

ng service (service)

Create a service command: ng G Service Services / Service Name +

Note the use of services:

  Using (Services) service requires, module.ts (root module) incorporated in app. And configure

  1. import { storageService } form './services/storage.service';
  2. Configuration [storage.service] providers in the
  3. That page in the page is first referenced during the     import {storageService} form '../../services/storage.service
  4. Statement way:
    1. A method var storage = new storages.service ();
    2. Method II using the constructor constructor (public storage: storageservice) {let s = this.storage.get (); console.log (s);}

  constructor (public storage: stroageservice) sentence is equivalent to storage = new storageservice ()

 

  this.storage.get () method is get a statement calling the service storage in

 

  The same storageservice storageservice must be introduced class name: public storage


 

storage.service中

  set(key:string,value:any){

    loacalstorage.setitem(key,json.stringify(value));

  }

  get(key:string){

    return Json.parse(localStorage.getItem(key))

  }

  remove(key:string){

    localstorage.removeItem(key);

  }

 

 

 

 

  

Guess you like

Origin www.cnblogs.com/rockyjs/p/11274882.html