执行顺序
发布于 6 年前 作者 xiulan38 14958 次浏览 来自 问答

如何控制小程序的执行顺序,比如我在onLaunch方法里面请求服务器,在首页onLoad里面要用到在onLaunch里面的返回值,但是onLoad执行的时候onLaunch还没有返回值,如何保证等onLaunch执行完成后再执行onLoad方法

3 回复

那就把flag  定义在app里啊  通过getApp(); 拿到的app 和 app.js里面的是同一个实例

init  可以注册到app里面 也可以直接写在app里面

Page({

flag:1,//这个东东就是用来标志的

onLaunch(){

    

request(( res )=>{

        if(this.flag==2){//说明onload已经执行了

            this.init();

        }else{

            this.flag = 2;

        }


    

})

},

onLoad(){

    

if(this.flag==2) {

    

    this.init();

    

}else{

    

this.flag = 2;//表示onload已经执行咯

    

}

},

init(){//这里就是你想在onload里面执行的东东

}

})

onLaunch是在app.js里面的方法,onLoad是在index.js里面的方法,它们不是在一个页面的方法

回到顶部