Ionic3 新增 Service


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();
  }

}

   

原創文章,歡迎轉載,轉載請注明出處!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM