Angular services and data persistence

1. Service roles:

  To solve problems between different components not like to call each other on the service so that all components are common methods can be used.

Create a service order

ng g service my-service 

time to create the specified directory 
ng g service services / storage

 1> is introduced and configured service

In the app.module.ts

import {name} from 'path'

Configuration []: In providers

2> write method in the services file

3> in the corresponding component to invoke a method

Introducing ---- --- you can print to initialize method ( this method is not recommended )

Recommended method

Introduced the same way

Claims Services

 2. Data persistence (localstorage h5 in)

We will get the data can be put in localstorage

When the page refreshes, to obtain data to determine whether there is localstorage

 note:

Components can call methods in the service, but the service can not be invoked between components, assemblies and components can not call each other, but between the service and the service they can call each other 

 

Guess you like

Origin www.cnblogs.com/z-ling/p/studyhouse_angular2.html