1.前言
最近開發(fā)小程序,開發(fā)環(huán)境uni_app+vue3等。在獲取小程序平臺(tái)用戶信息是報(bào)錯(cuò):
{errMsg: "getUserProfile:fail must be invoked by user tap gesture", errNo: 21500}
我在抖音上查了下錯(cuò)誤碼:
看到這個(gè)解釋也是瞬間無語了,然后在平臺(tái)查找錯(cuò)誤,找了半天終于在vue2 升級(jí)vue3文檔里面找到解決辦法,官方鏈接:
uni-app官網(wǎng) (dcloud.io)https://zh.uniapp.dcloud.io/tutorial/migration-to-vue3.html#some-miniapp-terminal-events-of-vue3-project-are-delayed-or-failed-to-call
這里也給出了解釋。
2.代碼邏輯
<template>
<view class="page">
<view class="box" data-eventsync="true" @click="getUserProfile">
<text class="head">頭像</text>
<text>獲取用戶信息</text>
</view>
<button data-eventsync="true" @click="getUserProfile">獲取用戶信息</button>
</view>
</template>
<script setup lang="ts">
const getUserProfile = async () => {
await new Promise(() => {
uni.getUserProfile({
success: (res) => {
console.log('success', res);
}, fail: (err) => {
console.log('err', err);
uni.showModal({
title: '提示',
content: JSON.stringify(err),
success: function (res) {
if (res.confirm) {
} else if (res.cancel) {
}
}
});
}
})
})
}
</script>
注意點(diǎn):一定要把data-eventsync="true"添加到事件源上,冒泡到父節(jié)點(diǎn)不會(huì)生效,如下圖所示,在外層view添加了點(diǎn)擊事件,我點(diǎn)擊了子元素,也會(huì)報(bào)錯(cuò),所以子結(jié)點(diǎn)上也要添加data-eventsync="true"。
完整代碼文章來源:http://www.zghlxwxcb.cn/news/detail-765308.html
<template>
<view class="page">
<view class="box" data-eventsync="true" @click="getUserProfile">
<text class="head" data-eventsync="true">頭像</text>
<text data-eventsync="true">獲取用戶信息</text>
</view>
<button data-eventsync="true" @click="getUserProfile">獲取用戶信息</button>
</view>
</template>
<script setup lang="ts">
const getUserProfile = async () => {
await new Promise(() => {
uni.getUserProfile({
success: (res) => {
console.log('success', res);
}, fail: (err) => {
console.log('err', err);
uni.showModal({
title: '提示',
content: JSON.stringify(err),
success: function (res) {
if (res.confirm) {
} else if (res.cancel) {
}
}
});
}
})
})
}
</script>
以上是uni_app加vue3開發(fā)遇到的問題,希望對(duì)你有所幫助。vue2開發(fā)小程序不會(huì)存在這個(gè)問題。文章來源地址http://www.zghlxwxcb.cn/news/detail-765308.html
到了這里,關(guān)于uni_app+vite+vue3+ts開發(fā)小程序,解決getUserProfile()獲取用戶信息問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!