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
- import { storageService } form './services/storage.service';
- Configuration [storage.service] providers in the
- That page in the page is first referenced during the import {storageService} form '../../services/storage.service
- Statement way:
- A method var storage = new storages.service ();
- 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);
}