本文介绍了如何在 Ionic 2 中使用 angular 2 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Ionic 2 的新手.我阅读了 angular 2 文档,该服务需要在引导应用程序时注入.但是在学习 Ionic 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.
非常感谢任何帮助.
推荐答案Ionic2 中没有使用 Bootstrap(),只使用 @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() } }更多推荐
如何在 Ionic 2 中使用 angular 2 服务?
发布评论