//app.js中
onLaunch(options) {
//调用获取用户配置
this.getUserConfigs()
},
configPromise: null,
userConfigs:null
/**
* 获取配置信息
*/
getUserConfigs() {
// 如果已经有Promise在执行,直接返回该Promise
if (this.configPromise) {
return this.configPromise;
}
// 创建新的Promise
this.configPromise = new Promise(async (resolve, reject) => {
try {
const res = await wechatConfigApi();
this.userConfigs = configs.initData(res);
resolve(this.userConfigs);
console.log('-------------------',this.userConfigs);
} catch (err) {
wx.utils.toast('获取服务器配置获取失败!!!,当前小程序出错了,请重新进入');
reject(err);
}
});
return this.configPromise;
},
//index.js中
async onLoad(options) {
console.log('我是页面onload');
try {
//接收promise 这样的话就会先调用getUserConfigs了
await app.getUserConfigs();
} catch (error) {
wx.utils.toast('配置加载失败:', error)
}
},
微信小程序开发onLaunch异步 onLoad获取参数失败解决方式
最新推荐文章于 2025-12-16 12:06:21 发布
340

被折叠的 条评论
为什么被折叠?



