AngularJS2 学习笔记——创建service

来源:互联网 发布:seo黑帽很花钱吗 编辑:程序博客网 时间:2024/06/16 17:47

1. 创建文件

服务的名称使用小写,格式如:
constants.service.ts

import { Injectable } from '@angular/core';@Injectable()export class ConstantsService {    abcd = 'test';}

位置假设放在:
app/constants.service.ts

2. 修改app.module.ts

import { ConstantsService } from './constants.service';
providers:[    ConstantsService]

3. 使用

在要使用的地方,假设src/page/auth/login.ts

import { ConstantsService } from '../../app/constants.service';@Component({  templateUrl: 'login.html',  providers:[ConstantsService]})export class LoginPage {  constructor(public navCtrl: NavController,private http: Http, private constantsService: ConstantsService) {  login(user_name: HTMLInputElement, password: HTMLInputElement){   console.info('constantsService',this.constantsService.abcd);  }}
原创粉丝点击