zoukankan      html  css  js  c++  java
  • 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();
      }
    
    }

       

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

  • 相关阅读:
    中台之交付
    mysql之事务
    中台之中台的设计
    0318 guava并发工具
    0312 java接口测试三棱军刺rest-assured
    0309 软件基本原理1
    0308 软件系统的非功能需求
    PELT(Per-Entity Load Tracking)
    CPU亲和度
    硬件相关知识随手笔记
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/ionicService.html
Copyright © 2011-2022 走看看