官术网_书友最值得收藏!

Injectable services

Now, let's create the boilerplate we need for our services. Note here that the injectable decorator is imported from Angular to declare that our service will be made available through Angular's Dependency Injection (DI) system, which allows these services to be injected into any class constructor that may need it. The DI system provides a nice way to guarantee that these services will be instantiated as singletons and shared across our app. It's also worth noting that we could alternatively provide these services on the component level if we didn't want them to be singletons and instead have unique instances created for certain branches of our component tree, which will make up our user interface. In this case, though, we want these created as singletons. We will be adding the following to our CoreModule:

  • LogService: Service to funnel all our console logging through.
  • DatabaseService: Service to handle any persistent data our app needs. For our app, we will implement the native mobile device's storage options, such as application settings, as a simple key/value store. However, you could implement more advanced storage options here, such as remote storage through Firebase for example.

Create app/modules/core/services/log.service.ts:

// angular
import { Injectable } from '@angular/core';
@Injectable()
export class LogService {
}

Also, create app/modules/core/services/database.service.ts:

// angular
import { Injectable } from '@angular/core';
@Injectable()
export class DatabaseService {
}
主站蜘蛛池模板: 台湾省| 中牟县| 汝南县| 普陀区| 东港市| 行唐县| 旅游| 和平县| 珲春市| 靖江市| 克什克腾旗| 阿鲁科尔沁旗| 福鼎市| 阿拉善盟| 英吉沙县| 海原县| 山阳县| 印江| 冕宁县| 原阳县| 扶风县| 施甸县| 深圳市| 广西| 磴口县| 马山县| 永嘉县| 娄底市| 运城市| 华亭县| 西乌珠穆沁旗| 邹城市| 泗水县| 日照市| 秭归县| 修文县| 合水县| 临江市| 建昌县| 仪陇县| 伊吾县|