码迷,mamicode.com
首页 > 其他好文 > 详细

Ionic3 新增 Service

时间:2017-12-28 19:52:51      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:const   temp   ati   文件中   import   blog   nav   post   cto   

新增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

标签:const   temp   ati   文件中   import   blog   nav   post   cto   

原文地址:https://www.cnblogs.com/acm-bingzi/p/ionicService.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!