AngularJS2 学习笔记——创建service
发布时间:2020-12-17 08:42:09 所属栏目:安全 来源:网络整理
导读:1. 创建文件 服务的名称使用小写,格式如: constants.service.ts import { Injectable } from '@angular/core' ; @Injectable () export class ConstantsService { abcd = 'test' ;} 位置假设放在: app/constants.service.ts 2. 修改app.module.ts import
1. 创建文件服务的名称使用小写,格式如: import { Injectable } from '@angular/core';
@Injectable()
export class ConstantsService {
abcd = 'test';
}
位置假设放在: 2. 修改app.module.tsimport { 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);
}
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |