uni_app+vite+vue3+ts开发小程序,解决getUserProfile()获取用户信息问题

1.前言

最近开发小程序,开发环境uni_app+vue3等。在获取小程序平台用户信息是报错:

{errMsg: "getUserProfile:fail must be invoked by user tap gesture", errNo: 21500}

我在抖音上查了下错误码:

看到这个解释也是瞬间无语了,然后在平台查找错误,找了半天终于在vue2 升级vue3文档里面找到解决办法,官方链接:

uni-app官网 (dcloud.io)icon-default.png?t=N7T8https://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>

注意点:一定要把data-eventsync="true"添加到事件源上,冒泡到父节点不会生效,如下图所示,在外层view添加了点击事件,我点击了子元素,也会报错,所以子结点上也要添加data-eventsync="true"。

完整代码

<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开发遇到的问题,希望对你有所帮助。vue2开发小程序不会存在这个问题。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
搭建一个使用vue3+ts+vite+uniapp的微信小程序的步骤如下: 1. 首先安装最新版的Node.js和npm。 2. 安装uni-app-cli脚手架工具,命令如下: ``` npm install -g @vue/cli npm install -g @vue/cli-init npm install -g @dcloudio/uni-cli ``` 3. 创建一个uni-app项目,命令如下: ``` vue create -p dcloudio/uni-preset-vue my-project ``` 4. 进入项目目录,安装依赖包,命令如下: ``` cd my-project npm install ``` 5. 安装并配置微信小程序开发者工具,下载地址:https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html 6. 在微信小程序开发者工具中,选择导入uni-app项目,选择项目目录下的dist/dev/mp-weixin文件夹,导入后即可进行开发和调试。 7. 如果需要使用vue3和typescript,在项目中安装相关依赖包,命令如下: ``` npm install --save-dev vue@next @vue/compiler-sfc typescript ts-loader ``` 8. 在项目根目录下创建vue.config.js文件,配置如下: ``` module.exports = { chainWebpack: config => { config.module .rule('ts') .use('ts-loader') .loader('ts-loader') .tap(options => { options.appendTsSuffixTo = [/\.vue$/] return options }) } } ``` 9. 在src目录下创建shims-vue.d.ts文件,内容如下: ``` declare module "*.vue" { import { ComponentOptions } from "vue"; const component: ComponentOptions; export default component; } ``` 10. 现在你就可以使用vue3和typescript进行开发了。同时,如果需要使用vite进行开发,可以参考uni-app官方文档进行配置:https://uniapp.dcloud.io/collocation/vite 以上就是使用vue3+ts+vite+uniapp搭建微信小程序的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值