網站首頁 編程語言 正文
在小程序中,我們一般在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-10-29 SQL Server常用的函數與查詢方法
- 2022-11-17 Android使用元數據實現配置信息的傳遞方法詳細介紹_Android
- 2022-04-01 SQL?Server的存儲過程詳解_MsSql
- 2022-06-19 WPF項目在設計界面調用后臺代碼_實用技巧
- 2022-09-15 React手寫一個手風琴組件示例_React
- 2023-01-18 C#實現圖片輪播功能的示例代碼_C#教程
- 2023-07-05 React hooks之useCallback的使用與性能分析
- 2022-05-17 ubuntu No package ‘libzmq‘ found
- 最近更新
-
- 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同步修改后的遠程分支