- NativeScript for Angular Mobile Development
- Nathan Walker Nathanael J. Anderson
- 226字
- 2021-07-02 18:41:47
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 {
}
- TypeScript入門與實(shí)戰(zhàn)
- Visual FoxPro程序設(shè)計(jì)教程(第3版)
- Python數(shù)據(jù)分析基礎(chǔ)
- Mastering Unity Shaders and Effects
- Mastering Swift 2
- Responsive Web Design by Example
- 程序是怎樣跑起來的(第3版)
- QGIS By Example
- Scala Reactive Programming
- Python圖形化編程(微課版)
- Scratch趣味編程:陪孩子像搭積木一樣學(xué)編程
- 大數(shù)據(jù)時代的企業(yè)升級之道(全3冊)
- 寫給青少年的人工智能(Python版·微課視頻版)
- Moodle 3.x Developer's Guide
- C#教程