日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

created不能异步_mpvue里created里异步请求结果,如何在beforeMount里获取到呢

發(fā)布時(shí)間:2023/12/19 vue 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 created不能异步_mpvue里created里异步请求结果,如何在beforeMount里获取到呢 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題描述

用mpvue做小程序時(shí),要在首頁展示輪播圖之前獲取到token,openId,并在請(qǐng)求獲取輪播圖時(shí)候在header里傳進(jìn)去,后面所有接口都需要這兩個(gè)頭部信息。我想在created里獲取到這兩個(gè)結(jié)果,存在storage里在beforeMount里調(diào)用,可是不能行

之前我是直接用new Promise一路then下來,最后一個(gè)then里調(diào)用輪播方法傳進(jìn)去openId和token,想問下還有別的方法嗎

this.$http是flyIO掛載在vue原型上

created(){

this.checkIt()

.then(function (res) {

console.log('index首頁登錄----成功')

var code = wx.getStorageSync('code')

var appId = wx.getStorageSync('appId')

var openId = wx.getStorageSync('openId')

var token = wx.getStorageSync('token')

console.log(code, appId, openId, token)

})

.catch(function (res) {

console.log('失敗')

that.login()

.then(function (res) {

console.log(res)

that.getId(res)

.then(that.$http.spread(function (res1, res2) {

// 兩個(gè)請(qǐng)求都完成

var openId = res1.data.data.open_id

var token = res2.data.data

console.log(openId, token)

wx.setStorageSync('openId', openId)

wx.setStorageSync('token', token)

// 最后,調(diào)用輪播等接口傳進(jìn)去openId和token,有沒有其他封裝方法

}))

.catch(function (error) {

console.log(error)

})

}).catch(function (err) {

console.log(err)

})

})

},

methods: {

checkIt () {

return new Promise((resolve, reject) => {

wx.checkSession({

success (res) {

resolve(res)

},

fail (res) {

reject(res)

}

})

})

},

login () {

return new Promise((resolve, reject) => {

wx.login({

success (res) {

resolve(res)

},

fail (err) {

reject(err)

}

})

})

},

getId (res) {

return new Promise((resolve, reject) => {

// 成功后獲取code

let code = res.code

console.log('code碼是:-->' + code)

var accountInfo = wx.getAccountInfoSync()

var appId = accountInfo.miniProgram.appId

console.log('appId碼是:-->' + appId)

wx.setStorageSync('code', code)

wx.setStorageSync('appId', appId)

var url = baseUrl+'/shop/weixin/login?code=' + code + '&appId=' + appId

var url2 = baseUrl+'/login/userInfo?appId=' + appId

return this.$http.all([this.getOpenId(url), this.getToken(url2)])

.then((res1, res2) => {

resolve(res1, res2)

})

}

有沒有解決方法是,加載完了created后,這些信息存在storage里,在beforeMount里能取到值,而不是都在then里

總結(jié)

以上是生活随笔為你收集整理的created不能异步_mpvue里created里异步请求结果,如何在beforeMount里获取到呢的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。