service是单例模式的

新增Service类 search.service.ts

import {Injectable} from '@angular/core';

@Injectable()
export class SearchService {
searchArea: string;
constructor() {
this.searchArea='广州市';
}
}

新增Service模块 service.module.ts

import {NgModule} from '@angular/core';
import {SearchService} from "./search.service"; const services = [
SearchService
]; @NgModule({
imports: [],
exports: [],
declarations: [],
providers: [...services]
})
export class ServiceModule {
}

修改App根模块 app.module.ts

import {NgModule} from '@angular/core';
import {ServiceModule} from "./service/service.module"; @NgModule({
declarations: [],
imports: [
ServiceModule
],
bootstrap: [],
entryComponents: [],
providers: []
})
export class AppModule { }

在其他ts文件中使用该Service的方法

import {Component} from '@angular/core';
import {NavController} from "ionic-angular";
import {SearchService} from "../../../app/service/search.service"; @Component({
templateUrl: 'choice.html',
}) export class SearchChoicePage {
constructor(public navCtrl: NavController, private searchService: SearchService) {
} returnArea(name: string) {
this.searchService.searchArea = name;
this.navCtrl.pop();
} }

  

原创文章,欢迎转载,转载请注明出处!

最新文章

  1. 元素绝对定位以后设置了高宽,a标签不能点击的原因总结
  2. AES加密类
  3. PowerDesigner新装后的设置
  4. 并发异步处理队列 .NET 4.5+
  5. pcap文件格式及文件解析
  6. Mvc 自带分页控件PagedList.Mvc Demo示例
  7. boot2docker安装及使用
  8. 实现一个Memcpy函数:将源指针所指的区域从起始地址开始的n个字节复制到目的指针所指区域
  9. 解决项目中找不到Maven Dependencies
  10. 如何运行容器?- 每天5分钟玩转 Docker 容器技术(22)
  11. SpringMVC归纳-2(Session会话、拦截器)
  12. C#3.0智能的编译器
  13. CMOS门电路
  14. 【转】gcc命令中参数c和o混合使用的详解
  15. [HAOI 2016]找相同字符
  16. Android Studio 导入工程
  17. CentOS7 虚拟机设置文件共享 VMWareTools安装遇到的坑
  18. 用AndroidStudio创建so
  19. linux服务器关闭ipv6 方法
  20. HDU 2647 Reward(拓扑排序,vector实现邻接表)

热门文章

  1. C++入门之初话多态与虚函数
  2. Python学习之赋值列表
  3. Stack Sorting CodeForces - 911E (思维+单调栈思想)
  4. [2017BUAA软工助教]常见问题Q&A
  5. MyBatis使用注解开发
  6. 爬虫——selenium基础
  7. cmake : undefined reference to dlopen, dlclose, dlsym and dlerror
  8. K3CLOUD数据权限授权
  9. Gevent 性能和 gevent.loop 的运用和带来的思考
  10. vue開發環境搭建