如何使用离子2角的用户服务2 [英] How to user angular 2 service with Ionic 2
本文介绍了如何使用离子2角的用户服务2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的离子2。
我读了2角的文档,该服务的需求,同时引导应用程序被注入。但看不到任何东西引导要通过离子2教程一会儿。
I am new to Ionic 2. I read in angular 2 docs, that service needs to be injected while bootstrap application. But could not see any bootstrap thing while going through Ionic 2 tutorial.
任何帮助是非常AP preciated。
Any help is highly appreciated.
推荐答案
有没有用引导的()在Ionic2,只用@App的声明你的应用程序。
你仍然需要在@Page部件声明为您服务。
There is no use of Bootstrap() in Ionic2, only use of @App to declare your app. You still need to declare your service in your @Page component.
创建服务
import {Injectable} from "angular2/core";
import {Http} from "angular2/http";
@Injectable()
export class DataService {
constructor(http: Http) {
this.http = http;
this.data = null;
}
retrieveData() {
this.http.get('./mocks/test.json')
.subscribe(data => {
this.data = data;
});
}
getData() {
return this.data;
}
}
然后在你的@Page使用它
Then use it in your @Page
import {Page} from 'ionic/ionic';
import {DataService} from './service';
@Page({
templateUrl: 'build/test.html',
providers: [DataService]
})
export class TestPage {
constructor(data: DataService) {
data.retrieveData()
}
}
这篇关于如何使用离子2角的用户服务2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文