uni-app vue3 微信小程序端不支持v-model:xxx属性双向绑定
uni-app vue3 微信小程序端不支持v-model:xxx属性双向绑定
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Mac | Mac big sur 11.6.1 | HBuilderX |
产品分类:uniapp/小程序/微信
PC开发环境操作系统:Mac
HBuilderX类型:Alpha
HBuilderX版本号:3.2.15
第三方开发者工具版本号:1.05.2110290
基础库版本号:2.21.0
项目创建方式:HBuilderX
示例代码:
<i-bottom-popup v-model:show="showBottomPopup">
<view style="height:400rpx;">
<text>测试v-model:xxx语法</text>
</view>
</i-bottom-popup>
<template>
<view @touchmove.stop.prevent>
<view
class="tui-bottom-popup"
:class="{ 'tui-popup-show': show, 'tui-popup-radius': radius }"
:style="{ backgroundColor: backgroundColor, height: height ? height + 'rpx' : 'auto', zIndex: zIndex, transform: `translate3d(0, ${show ? translateY : '100%'}, 0)` }"
>
<slot></slot>
</view>
<view v-if="mask" class="tui-popup-mask" :class="[show ? 'tui-mask-show' : '']" :style="{ zIndex: maskZIndex }" @tap="handleClose"></view>
</view>
</template>
<script>
import { defineComponent, computed, watch } from 'vue'
export default defineComponent({
name: 'iBottomPopup',
emits: ['update:show', 'close'],
props: {
//是否需要mask
mask: {
type: Boolean,
default: true
},
//控制显示
show: {
type: Boolean,
default: false
},
//背景颜色
backgroundColor: {
type: String,
default: '#fff'
},
//高度 rpx
height: {
type: Number,
default: 0
},
//设置圆角
radius: {
type: Boolean,
default: true
},
zIndex: {
type: [Number, String],
default: 997
},
maskZIndex: {
type: [Number, String],
default: 996
},
//弹层显示时,垂直方向移动的距离
translateY: {
type: String,
default: '0'
}
},
setup(props, context) {
const { emit } = context
watch(
() => props.show,
nVal => {
console.log('watch: ', nVal)
emit('update:show', nVal)
}
)
const handleClose = () => {
if (!props.show) {
return
}
emit('update:show', false)
}
return {
handleClose
}
}
})
</script>
<style lang="scss" scoped>
.tui-bottom-popup {
width: 100%;
position: fixed;
left: 0;
right: 0;
bottom: 0;
opacity: 0;
transform: translate3d(0, 100%, 0);
transform-origin: center;
transition: all 0.3s ease-in-out;
min-height: 30rpx;
}
.tui-popup-radius {
border-top-left-radius: 24rpx;
border-top-right-radius: 24rpx;
padding-bottom: env(safe-area-inset-bottom);
overflow: hidden;
}
.tui-popup-show {
opacity: 1;
}
.tui-popup-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.6);
transition: all 0.3s ease-in-out;
opacity: 0;
visibility: hidden;
}
.tui-mask-show {
opacity: 1;
visibility: visible;
}
</style>
更多关于uni-app vue3 微信小程序端不支持v-model:xxx属性双向绑定的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
后续优化,已加分,感谢您的反馈!
更多关于uni-app vue3 微信小程序端不支持v-model:xxx属性双向绑定的实战教程也可以访问 https://www.itying.com/category-93-b0.html
更新至最新HBuilderX Alpha
在 uni-app 的 Vue 3 环境下,微信小程序端确实不支持 v-model:xxx 这种 Vue 3 特有的参数化 v-model 语法。这是由于微信小程序的自定义组件机制与 Vue 3 的编译兼容性限制。
问题原因:
v-model:show 是 Vue 3 的语法糖,它会被编译为 :show 和 @update:show 的组合。但在微信小程序平台,uni-app 的编译器需要将 Vue 组件转换为小程序自定义组件,而小程序原生不支持这种事件绑定语法。
解决方案:
- 改用传统
v-model:如果绑定的是value属性,可直接用v-model。 - 手动拆分为属性和事件:将
v-model:show="showBottomPopup"替换为:<i-bottom-popup :show="showBottomPopup" @update:show="showBottomPopup = $event" > - 在组件内部调整:确保子组件通过
emit('update:show', value)触发更新,父组件用上述方式接收。
示例修正: 父组件中:
<i-bottom-popup
:show="showBottomPopup"
@update:show="showBottomPopup = $event"
>
<!-- 内容 -->
</i-bottom-popup>

