app.js和第一个页面是异步执行的吗
发布于 6 年前 作者 jlong 11276 次浏览 来自 问答

我简单说明一下,我在app.js里的onLaunch方法里写了一个请求,在执行的第一个页面(由json定义的第一个页面)的加载页面函数写了一个console.log( res);,然后我经过多次执行发现,有时候是app.js先执行完成,有什么是首页的先执行,并不是我以为的要app.js先执行完成后才执行首页的。有什么办法可以让它先执行完app.js的代码然后再执行首页的吗

10 回复

helper.js,想需要你自己创建啊。

我这边给你写一个简单的案例吧。

文件一 app.js

//app.js
App({
  onLaunch: function () {
  },
    cache: {
        getuserid: function () {
            return wx.getStorageSync('loginuserid');
        }
    },
    wx:{
        request_post: function (url, data) {
            return new Promise(function (resolve, reject) {
                wx.request({
                    url: url,
                    data: data,
                    method: 'POST',
                    header: { "Content-Type": "application/x-www-form-urlencoded" },
                    success: function (res) {
                        resolve(res)
                    },
                    fail: function (err) {
                        reject(err)
                    }
                });
            });
        }
    }
})

文件二 helper.js

//utils/helper.js
var app = getApp();
function weixinauth() {
    return new Promise(function (resolve, reject) {
        if (app.cache.getuserid() > 0) {
            resolve("无需登录")
        } else {
            wx.login({
                success: function (res) {
                    if (!res.code)
                        return;
                    //##################################
                    app.wx.request_post('https://api.xxx.com/api/weixin/auth', {
                        code: res.code,
                    }).then(function (res) {
                        wx.setStorageSync("loginuserid", res.data.userid);
                        resolve(res.data)
                    }).catch(function (err) {
                        reject(err.data)
                    })
                    //##################################
                }
            });
        }
        //##################################
    });
}
module.exports.weixinauth = weixinauth;

文件三 index.js-------就是需要登录页面的调用

// pages/index.js
var helper = require("../utils/helper.js")
var that;
var app = getApp();
Page({
  data: {
   
  },
    onLoad: function (options) {
        that = this;
        helper.weixinauth().then(function (resdata) {
            console.log("resdata");
            that.GoLoad();
        }).catch(function (errdata) {
            console.log("errdata");
        })
  },
    GoLoad:function(){
        //这里写加载页面的方法
        var userid = app.cache.getuserid();
        console.log("GoLoad");
    }
})

我后面是打算这样做,之前我是怕它还是会发送多次请求,后来我思考了一下,可以用定时器,我的思路是这样的,在初始页面放一个定时器,每0.5秒扫描一次我登录的状态,然后登录状态符合要求了我才进去首页

是啊,这并不是解决方法,所以我给了他延迟加载。helper.js,是在那里呢

我设置2秒不是为了登录,而是为了让app.js里的执行完毕,因为我首页要进行判断值,这个值就是从app.js把值改变的,小程序的执行过程有问题,它应该等app.js执行完成后才执行首页的。

感谢4楼,我去试一下,@6楼,我是因为在app.js里我写了登录方法,它要返回一个参数状态给我,我会根据这个判断要决定跳转那个页面

之前我是这样做的,但是会出现一个问题,如果是首页先加载,那么它肯定会进入条件判断,这样就会多次执行APP。js的函数,

你的这个难道不是每次都要判断的吗?你把登录方法用promise方法单独写在一个apihelper.js文件中,然后没有页面load的时候需要判断,就调用就可以了啊

为什么搞那么麻烦呢。你每个需要登录页page的load里面加一个判断不就可以了吗?

正式页面之前添加一个初始化的页面,等待初始化完成,然后redirect加载要加载的页面,就像app的splash页面一样

还有一个就是分享的情况,能分享的页面都要等待登录的话,不采取promise等操作咋玩。。

楼主有好方法的时候记得分享一下哈

回到顶部