網站首頁 編程語言 正文
在小程序中,我們一般在onLaunch中發起登錄請求,由于是請求是異步的,在onLoad之后才執行完登錄請求,如果在onLoad中需要使用請求后的數據,會發現取不到值,以下是一些解決方式,可以按自己所需使用。
- 使用回調函數
// app.js
onLaunch () {
wx.login({
success: result => {
wx.request({
url: 'test.php' // 示例
data: {...},
success: res => {
this.globalData.openId = res.data.openId
// 回調
if (this.loginCallback) this.loginCallback(res.data)
},
fail: err => {
// 需要的話請求失敗處也可以使用回調
}
})
}
})
}
// page.js
onLoad () {
const app = getApp()
if (app.globalData.openId) {
// do something
} else {
app.loginCallback = (data) => {
// do something
}
}
}
- 使用Promise
// app.js
App({
initLogin () {
return new Promise((resolve, reject) => {
wx.login({
success: result => {
wx.request({
url: 'test.php' // 示例
data: {...},
success: res => {
resolve(res.data)
},
fail: err => {
reject('error')
}
})
}
})
})
}
})
// page.js
onLoad () {
const app = getApp()
app.initLogin().then(res => {
// do something
}).catch(err => {})
}
- 使用Object.defineProperty監聽
// app.js
watch (method) {
let obj = this.globalData
// 這里監聽 openId
Object.defineProperty(obj, "openId", {
configurable: true,
enumerable: true,
set: function (value) {
method(value);
},
get: function () {
return this.globalData
}
})
}
onLaunch () {
wx.login({
success: result => {
wx.request({
url: 'test.php' // 示例
data: {...},
success: res => {
this.globalData.openId = res.data.openId
}
})
}
})
}
// page.js
onLoad () {
const app = getApp()
app.watch(this.watchBack)
}
watchBack (openId) {
if (openId) {
// do something
}
}
- 使用getCurrentPages
// app.js
onLaunch () {
wx.login({
success: result => {
wx.request({
url: 'test.php' // 示例
data: {...},
success: res => {
const currentPage = wx.getCurrentPages()[0]
// 觸發當前頁的init方法
currentPage.init(res.data)
}
})
}
})
}
// page.js
init (data) {
// do somethine
}
- reLaunch重載頁面
// app.js
onLaunch () {
wx.login({
success: result => {
wx.request({
url: 'test.php' // 示例
data: {...},
success: res => {
// 請求結果存到了storage里,再跳到頁面可以獲取到storage里的數據
wx.reLaunch({
url: '/pages/n-index/n-index',
})
}
})
}
})
}
- 設置啟動頁
- …
你有什么更完美的解決方法,快在下方評論一起討論吧。
原文鏈接:https://blog.csdn.net/z291493823/article/details/121215584
- 上一篇:小程序滾動穿透解決方案
- 下一篇:正則判斷只能輸入大于0的正整數
相關推薦
- 2022-09-29 數據設計之權限的實現_數據庫其它
- 2022-11-03 C#事件中關于sender的用法解讀_C#教程
- 2023-01-19 C++?new與malloc和delete及free動態內存管理及區別介紹_C 語言
- 2022-08-13 Android實現加載圈_Android
- 2022-12-04 pytorch從頭開始搭建UNet++的過程詳解_相關技巧
- 2022-05-02 C語言遞歸實現歸并排序詳解_C 語言
- 2023-05-29 批標準化層?tf.keras.layers.Batchnormalization()解析_pytho
- 2021-12-17 C++基礎概念講述_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支