离子2全局变量

时间:2017-05-24 19:58:45

标签: ionic-framework ionic2

我有一个功能,在这个功能中我有:

var cake = {
  firstIngredient: "milk",
  secondIngredient: "eggs",
  thirdIngredient: "cakemix",
  bakeTime: 22
  bakeTemp: 420
  mixingInstructions: function() {
    return "Add "
    this.firstIngredient + " to " + this.secondIngredient + " and stir with " + this.thirdIngredient + " and bake at " + bakeTemp + " for " + bakeTime + " minutes.";
  }
};

这是我发现使用“本地化”,“param1”和& “param2”在同一时间,如果我这样做:

    this.geolocation.getCurrentPosition(posOptions).then((localisation,err) => {

        this.storage.get('param1').then((param1) => {

            this.storage.get('param2').then((param2) => {
            // Do some stuff with localisation, param1 & param2
            alert(localisation);
            alert(param1);
            alert(param2);              
            })

        })
    })

找不到本地化和param1

1 个答案:

答案 0 :(得分:0)

你可以在这里使用promise.all,因为params不是相互依赖的。 Promise.all接受一系列promise并等待所有promises返回,然后再执行then

let lPromise  =this.geolocation.getCurrentPosition(posOptions);
let p1Promise = this.storage.get('param1');
let p2Promise =  this.storage.get('param2');
Promise.all([lPromise,p1Promise,p2Promise])
.then((values) => {
        // Do some stuff with localisation, param1 & param2
        alert(values[0]);//localization
        alert(values[1]);//param1
        alert(values[2]);//param2    
    })
相关问题