Ionic3 新增 Service
2024-10-13 17:45:01
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();
} }
原创文章,欢迎转载,转载请注明出处!
最新文章
- 元素绝对定位以后设置了高宽,a标签不能点击的原因总结
- AES加密类
- PowerDesigner新装后的设置
- 并发异步处理队列 .NET 4.5+
- pcap文件格式及文件解析
- Mvc 自带分页控件PagedList.Mvc Demo示例
- boot2docker安装及使用
- 实现一个Memcpy函数:将源指针所指的区域从起始地址开始的n个字节复制到目的指针所指区域
- 解决项目中找不到Maven Dependencies
- 如何运行容器?- 每天5分钟玩转 Docker 容器技术(22)
- SpringMVC归纳-2(Session会话、拦截器)
- C#3.0智能的编译器
- CMOS门电路
- 【转】gcc命令中参数c和o混合使用的详解
- [HAOI 2016]找相同字符
- Android Studio 导入工程
- CentOS7 虚拟机设置文件共享 VMWareTools安装遇到的坑
- 用AndroidStudio创建so
- linux服务器关闭ipv6 方法
- HDU 2647 Reward(拓扑排序,vector实现邻接表)
热门文章
- C++入门之初话多态与虚函数
- Python学习之赋值列表
- Stack Sorting CodeForces - 911E (思维+单调栈思想)
- [2017BUAA软工助教]常见问题Q&;A
- MyBatis使用注解开发
- 爬虫——selenium基础
- cmake : undefined reference to dlopen, dlclose, dlsym and dlerror
- K3CLOUD数据权限授权
- Gevent 性能和 gevent.loop 的运用和带来的思考
- vue開發環境搭建