天天看點

ionic 2 添加service

1.myService.js

import {Injectable} from 'angular2/core';

@Injectable()

export class myService {

    constructor() {

    }

    public test() {

        console.log("test my service");

    }

}

2.app.js

import {myService} from './services/myService';

@App({

  templateUrl: 'app/app.html',

    providers: [myService]

})

3.MyComponent.js

import {myService} from './services/myService';

export class MyComponent {

constructor(public service: myService) {

}

}

上述方法在最新的RC版本中已經不再适用,請參考官方最新文檔。

注:

1.public service: myService等于

 this.service = service

2.如果引用Http,則需要同時引用HTTP_PROVIDERS