Javascript 如何执行&x27;http-get';承诺中的同步?角度2+;离子3

Javascript 如何执行&x27;http-get';承诺中的同步?角度2+;离子3,javascript,angular,ionic-framework,ionic3,Javascript,Angular,Ionic Framework,Ionic3,我不确定标题问题是否正确,因此我希望实现以下目标: 我试图将JSON数据保存在ionic的存储器中,我的函数返回一个承诺,因此我希望在这个函数中执行http.get以获取数据并保存在存储器中 saveStorage() { let promesa = new Promise((resolve, reject) => { //Check if user is in Mobile or Web if (this.platform.is("cordoba")) {

我不确定标题问题是否正确,因此我希望实现以下目标:

我试图将JSON数据保存在ionic的存储器中,我的函数返回一个承诺,因此我希望在这个函数中执行http.get以获取数据并保存在存储器中

saveStorage() {
    let promesa = new Promise((resolve, reject) => {
      //Check if user is in Mobile or Web
      if (this.platform.is("cordoba")) {
        this.storage.set('userJson', this.userObject);

        //dispositivo
        this.storage.set('password', this.user.password);
        this.storage.set('name',this.user.email);
      } else {
        //Desktop
        if (this.user.password) {
          //Save User
          localStorage.setItem("userJson", JSON.stringify(this.userObject));
          localStorage.setItem("password", this.user.password);
          localStorage.setItem("name", this.user.email);

          //save profiles to show menu depending profile
          let profiles=[];

          profiles.push({
            "name":"Admin",
            "profile":"user",
            "isActive":1,
            "page":"",
            "menu":""
          });
          /*THIS PART DOESNT WORK*/

          //Get another profiles from service

          let url='assets/data/userProfiles.json';

          let jsonArr=[];
          this.http.get(url).subscribe(data => {
            console.log(data);
            data[0].companies.forEach(function(cur){
              jsonArr.push({
                name: cur.name,
                profile: "company",
                isActive:0,
                page:"",
                menu:""
              });
            })
            data[1].centers.forEach(function(cur){
              jsonArr.push({
                name: cur.name,
                profile: "center",
                isActive:0,
                page:"",
                menu:""

              });
            })
            profiles.push(jsonArr);


          }, err => {
            console.log(err);
          });
          localStorage.setItem("profiles", JSON.stringify(profiles));
        } else {
          localStorage.removeItem("userJson");
          localStorage.removeItem("profiles");
          localStorage.removeItem("password");
          localStorage.removeItem("name");
        }
      }
    })
    return promesa;
  }
我现在使用json,因为URi的url将更改,json是:

[
    {
      "companies": [{
        "id": 1,
        "name": "Prueba",
        "company_number": "23423423A",
        "latitude": 241241.12,
        "longitude": 213213.12,
        "country": "ES"
      },
        {
          "id": 2,
          "name": "Prueba2",
          "company_number": "23423423A",
          "latitude": 241241.12,
          "longitude": 213213.12,
          "country": "US"
        },
        {
          "id": 3,
          "name": "Prueba3",
          "company_number": "23423423AB",
          "latitude": 241241.19,
          "longitude": 213213.20,
          "country": "US"
        }
      ]
    },
    {
      "centers":[
        {
          "id": 1,
          "name": "Prueba",
          "center_number": "23423423A",
          "latitude": 241241.12,
          "longitude": 213213.12,
          "country": "ES"
        },
        {
          "id": 2,
          "name": "Prueba2",
          "center_number": "23423423A",
          "latitude": 241241.12,
          "longitude": 213213.12,
          "country": "US"
        }
      ]
    }
  ]
当我检查它时,结果是:

[{“名称”:“管理员”,“档案”:“用户”,“isActive”:1,“页面”:“菜单”:“}]

它不会添加其他配置文件


我这样做是为了更改菜单和配置文件图片,这取决于用户在单击“菜单更改”按钮时是否选择了另一个配置文件,它将显示active=0的配置文件的模式,如果您知道另一种方法,请告诉我创建角度全局变量。

要使请求生效,将您的请求放入服务中并呼叫它

如果您不知道如何创建服务以及它应该如何工作,请查看的服务部分。但现在这里是如何做到这一点

将您的请求放入服务:

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

@Injectable()
export class MyService
{
  constructor(private http : Http){}

  public myRequest() {
    this.http.get(myUrl).map((res: Response) =>
    {
      return res.json();
    })
    .catch((error: any) => Observable.throw(error));
  }
}
将您的服务放在模块的“提供者”部分,例如
app.module.ts

providers: [ MyService ]
然后从组件中的服务调用myRequest:

...
constructor(private myService : MyService){}

this.myService.myRequest().subscribe(
  (res) =>
  {
    // do what you want
  },
  (err) =>
  {
    // error
  }
);

即使您是否回复了承诺,它也会起作用。

hello@Powkachu,谢谢您的回复,您能写一封服务信来说明具体的想法吗?