uni-app Vue3 Attribute 小程序 BUG

uni-app Vue3 Attribute 小程序 BUG

开发环境 版本号 项目创建方式
uniapp Vue2 CLI

产品分类:uniapp/App
PC开发环境操作系统:Mac
PC开发环境操作系统版本号:12
手机系统:全部
手机厂商:华为
页面类型:vue
打包方式:云端

测试过的手机:目前只有 H5 与 小程序

示例代码:

"@dcloudio/uni-app": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-app-plus": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-components": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-h5": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-mp-alipay": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-mp-baidu": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-mp-kuaishou": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-mp-lark": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-mp-qq": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-mp-toutiao": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-mp-weixin": "3.0.0-alpha-3040920220506001",
"@dcloudio/uni-quickapp-webview": "3.0.0-alpha-3040920220506001",

操作步骤:

请看描述

预期结果:

小程序正常显示父组件传过去的 Attribute , 目测 属性 、class 、style 都不展示

实际结果:

无样式

bug描述:

环境 vite / vue3 / uniapp 环境
看了看文档,Vue 3 的Attribute 小程序是支持的, 只有多级的 v-bind 不支持。
实际用下来发现并不支持 Attribute , 所以是 不支持还是说bug 呢

<Card style="background: red" />

更多关于uni-app Vue3 Attribute 小程序 BUG的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

我也遇到了这个问题,在最新版uni下 运行 vue3,H5模式倒是没啥问题; 组件: <template>
<view class="test-demo">
111111
</view>
</template>

<script> // #ifdef MP-WEIXIN export default { options: { virtualHost: true, addGlobalClass: true, }, }; // #endif </script> <style lang="scss"> </style>

页面使用:
<view class="content">
<Test class="custom" :style="{fontSize: '30rpx'}" />
</view> 期望效果:

未开启 virtualHost: true 前透传了,但是由于多了层节点;

开启 virtualHost: true后,没有了多余节点,但是class跟style的透传也都失效了;

本来想着用useAttrs 解决,但是发现小程序端无论怎样 打印出来都是没有值的; 只能暂时用自定义的props: cusClass 跟 cusStyle 传参再赋值解决了,但是写法显得很不自由; 不知道这是bug还是因为微信小程序的问题;

更多关于uni-app Vue3 Attribute 小程序 BUG的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在使用 uni-app 开发小程序时,可能会遇到一些与 Vue3 相关的 Attribute BUG。以下是一些常见的问题及其解决方案:

1. v-bind 动态绑定问题

  • 问题描述: 在 Vue3 中,使用 v-bind 动态绑定属性时,可能会遇到小程序端无法正确解析的问题。
  • 解决方案: 确保在 v-bind 中使用正确的语法,并且避免在小程序端使用不支持的属性。可以尝试使用 :prop="value" 的简写形式。
<template>
  <view :class="dynamicClass">Hello World</view>
</template>

<script setup>
const dynamicClass = ref('active');
</script>

2. v-model 双向绑定问题

  • 问题描述: 在小程序端,v-model 可能无法正常工作,尤其是在自定义组件中。
  • 解决方案: 确保在自定义组件中正确实现 modelValueupdate:modelValue 事件。如果问题仍然存在,可以尝试使用 @input 事件手动更新值。
<template>
  <custom-input v-model="inputValue" />
</template>

<script setup>
const inputValue = ref('');
</script>

3. v-for 渲染问题

  • 问题描述: 使用 v-for 渲染列表时,可能会遇到小程序端渲染不正确或性能问题。
  • 解决方案: 确保在 v-for 中使用 :key 属性,并且避免在循环中使用复杂的表达式。如果列表数据较大,可以考虑使用 virtual-list 组件来优化性能。
<template>
  <view v-for="item in items" :key="item.id">{{ item.name }}</view>
</template>

<script setup>
const items = ref([{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }]);
</script>

4. v-ifv-show 问题

  • 问题描述: 在小程序端,v-ifv-show 的行为可能与预期不一致,尤其是在动态切换时。
  • 解决方案: 确保在 v-ifv-show 中使用正确的条件表达式。如果需要在频繁切换时保持性能,优先使用 v-show
<template>
  <view v-if="isVisible">Visible</view>
  <view v-show="isVisible">Also Visible</view>
</template>

<script setup>
const isVisible = ref(true);
</script>

5. refreactive 问题

  • 问题描述: 在小程序端,refreactive 可能无法正确响应数据变化。
  • 解决方案: 确保在 refreactive 中使用正确的数据类型,并且避免直接修改嵌套对象的属性。如果需要修改嵌套对象,可以使用 toRefswatch 来监听变化。
<template>
  <view>{{ state.count }}</view>
  <button @click="increment">Increment</button>
</template>

<script setup>
const state = reactive({ count: 0 });
const increment = () => {
  state.count++;
};
</script>

6. watchwatchEffect 问题

  • 问题描述: 在小程序端,watchwatchEffect 可能无法正确监听数据变化。
  • 解决方案: 确保在 watchwatchEffect 中使用正确的依赖项,并且避免在监听器中执行复杂的逻辑。如果问题仍然存在,可以尝试使用 computed 属性来替代。
<template>
  <view>{{ count }}</view>
</template>

<script setup>
const count = ref(0);
watch(count, (newValue, oldValue) => {
  console.log(`Count changed from ${oldValue} to ${newValue}`);
});
</script>

7. computed 计算属性问题

  • 问题描述: 在小程序端,computed 计算属性可能无法正确更新。
  • 解决方案: 确保在 computed 中使用正确的依赖项,并且避免在计算属性中执行副作用操作。如果问题仍然存在,可以尝试使用 watch 来监听依赖项的变化。
<template>
  <view>{{ doubleCount }}</view>
</template>

<script setup>
const count = ref(1);
const doubleCount = computed(() => count.value * 2);
</script>

8. provideinject 问题

  • 问题描述: 在小程序端,provideinject 可能无法正确传递数据。
  • 解决方案: 确保在 provideinject 中使用正确的键值,并且避免在嵌套组件中重复提供相同的数据。如果问题仍然存在,可以尝试使用全局状态管理工具(如 Pinia)来替代。
<template>
  <child-component />
</template>

<script setup>
import { provide, ref } from 'vue';
const message = ref('Hello from parent');
provide('message', message);
</script>

9. teleport 问题

  • 问题描述: 在小程序端,teleport 可能无法正确将组件渲染到目标位置。
  • 解决方案: 确保在 teleport 中使用正确的目标选择器,并且避免在小程序端使用不支持的 DOM 操作。如果问题仍然存在,可以尝试使用 v-ifv-show 来手动控制组件的显示。
<template>
  <teleport to="#target">
    <div>Teleported Content</div>
  </teleport>
</template>

10. transitiontransition-group 问题

  • 问题描述: 在小程序端,transitiontransition-group 可能无法正确应用动画效果。
  • 解决方案: 确保在 transitiontransition-group 中使用正确的 CSS 类名,并且避免在小程序端使用不支持的 CSS 属性。如果问题仍然存在,可以尝试使用小程序原生的动画 API 来替代。
<template>
  <transition name="fade">
    <view v-if="isVisible">Fade In/Out</view>
  </transition>
</template>

<script setup>
const isVisible = ref(true);
</script>

<style>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>
回到顶部