Ionic3 新增 Service

Posted 阿丙的博客园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ionic3 新增 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();
  }

}

   

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

 

以上是关于Ionic3 新增 Service的主要内容,如果未能解决你的问题,请参考以下文章

Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段

Android 新增外设Service添加供app使用

ionic3-ng4学习见闻--(多语言方案)

ionic内联模板 ionic3还在用吗?

ionic3-ng4学习见闻--(多环境方案)

VSCode插件开发全攻略代码片段设置自定义欢迎页