如何使用Ionic 2的角度2服务?

编程入门 行业动态 更新时间:2024-10-21 09:25:34
本文介绍了如何使用Ionic 2的角度2服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是Ionic 2的新手。我在有角度的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

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的角度2服务?

本文发布于:2023-10-09 06:10:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   角度   Ionic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!