我是ionic 3
的新手,我希望在我的ionic 3
中获取包含我的数据的JSON
所以我试着按照这个教程:
https://www.youtube.com/watch?v=vuc4dp0qHSc
Project ionic-info:
https://prnt.sc/ft60lb
但我在控制台中看不到任何数据:
https://prnt.sc/ft63rl
我的项目内部编辑: https://i.stack.imgur.com/0jyPu.jpg
和这里:package.json
{
"name": "ts-test",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"private": true,
"scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"lint": "ionic-app-scripts lint",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve"
},
"dependencies": {
"@angular/common": "4.1.3",
"@angular/compiler": "4.1.3",
"@angular/compiler-cli": "4.1.3",
"@angular/core": "4.1.3",
"@angular/forms": "4.1.3",
"@angular/http": "4.1.3",
"@angular/platform-browser": "4.1.3",
"@angular/platform-browser-dynamic": "4.1.3",
"@ionic-native/core": "3.12.1",
"@ionic-native/splash-screen": "3.12.1",
"@ionic-native/status-bar": "3.12.1",
"@ionic/storage": "2.0.1",
"ionic-angular": "3.5.0",
"ionicons": "3.0.0",
"rxjs": "5.4.0",
"sw-toolbox": "3.6.0",
"zone.js": "0.8.12"
},
"devDependencies": {
"@ionic/app-scripts": "1.3.12",
"@ionic/cli-plugin-ionic-angular": "1.3.1",
"typescript": "2.3.4"
},
"description": "An Ionic project"
}
这里是reddit-data.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the RedditDataProvider provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular DI.
*/
@Injectable()
export class RedditDataProvider {
constructor(public http: Http) {
console.log('Hello RedditDataProvider Provider');
}
getRemoteData(){
this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=10&sort=hot').map(res=>res.json()).subscribe(data => {
console.log(data);
});
}
}
这里:app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { RedditDataProvider } from '../providers/reddit-data/reddit-data';
import { HttpModule } from '@angular/http';
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
RedditDataProvider
]
})
export class AppModule {}
这里:home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { RedditDataProvider } from '../../providers/reddit-data/reddit-data';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(public navCtrl: NavController,public redditService:RedditDataProvider) {
}
ionicViewDidLoad(){
this.redditService.getRemoteData();
}
}
答案 0 :(得分:0)
在 home.ts
中尝试使用.subscribeionicViewDidLoad(){
this.redditService.getRemoteData().subscribe(res => {
console.log(res);
});
}
并添加提供商
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [RedditDataProvider]
})
reddit-data.ts 您可以更改为
getRemoteData(){
return this.http.get('https://www.reddit.com/r/gifs/top/.json?limit=10&sort=hot')
.map(res => res.json());
}