如何从 Ionic App 中的端点获取实时 JSON

how to get realtime JSON from endpoint in Ionic App

按照 url 的内容使用存储在 /assets/data 下的 JSON 文件实现动态菜单项。菜单在存储的 JSON 文件中工作正常。现在我需要从 Salesforce API 实时动态检索相同格式的 JSON 并显示其内容。 有人可以建议我需要在这里进行哪些更改吗? getMainMenu() 方法中的 json 路径是否应替换为实际的 Saleforce API?

下面是数据-service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/map';

/*
  Generated class for the DataServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class DataServiceProvider {

  constructor(public http: Http) {
    console.log('Hello DataServiceProvider Provider');
  }

  getMainMenu(){
    return this.http.get('assets/data/mainmenu.json')
    .map((response:Response)=>response.json().Categories);
  }

}

和app.component.ts

import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';

import { DataServiceProvider } from '../providers/data-service/data-service'

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;

  rootPage: any = HomePage;

  pages: any[]; //Array<{title: string, component: any}>;
  mainmenu: any[];
  
  constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, public dataService: DataServiceProvider) {
    this.initializeApp();

    this.dataService.getMainMenu().subscribe((Response)=>{
      this.mainmenu = Response;
      console.log(this.mainmenu);
    });

    // used for an example of ngFor and navigation
    this.pages = [
      { title: 'Home', component: HomePage },
      { title: 'List', component: ListPage }
    ];

  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }

  toggleSection(i) {
    this.mainmenu[i].open = !this.mainmenu[i].open;
  };

  toggleItem(i,j) {
    this.mainmenu[i].SubCategories[j].open = !this.mainmenu[i].SubCategories[j].open;
  };
}

您似乎需要将 getMainMenu 方法中的 url 更新为 api 中的方法。您可能需要进行一些其他更改,例如添加身份验证 headers,但如果来自 api 的数据与资产文件夹中存储的数据相同,则您的组件不应该care "where" 数据来自