29 Service

service:

In a large project, if there is some method or data, require repeated use, think of ways to package and facilitate reuse, angular recommended by encapsulated into an object -> Services

Service is a class that encapsulates some of the commonly used methods in this class or data

 

Case:

If a project has a very multiple components, test development, outputs log information to a bunch of easy commissioning; to publish online, one by one to delete the log information is too much trouble, you need to package a log class service, reducing the workload

 

Services using the steps:

① create a service

  @Injectable () // defines the data and methods

   

② to the designated service provider

  Two options: Option one: a module designated provider, all the components of the module may be introduced to use the service

       Option two: designated a component provider, the component sub-assembly and to use the service can be introduced

    import { LogService } from ' *** '

    providers : [ LogService ]

   

③ call service

  Is introduced: import {LogService} from '***'

  Examples of: constructor (private myService: LogService) {}

  Call: this.myService ** / ** ().

   

 

   

 

   

Guess you like

Origin www.cnblogs.com/shanlu0000/p/12229969.html
29