Vue 3 中 <transition-group> 组件报错的非 props 属性传递问题

简介: Vue 3 中 <transition-group> 组件报错的非 props 属性传递问题

更多ruoyi-nbcio功能请看演示系统

gitee源代码地址

前后端代码: https://gitee.com/nbacheng/ruoyi-nbcio

演示地址:RuoYi-Nbcio后台管理系统 http://218.75.87.38:9666/

更多nbcio-boot功能请看演示系统

gitee源代码地址

后端代码: https://gitee.com/nbacheng/nbcio-boot

前端代码:https://gitee.com/nbacheng/nbcio-vue.git

进入主页后报下面的警告信息

[Vue warn]: Extraneous non-props attributes (mode) were passed to component but could not be automatically inherited because component renders fragment or text root nodes. 
  at <TransitionGroup name="fade-transform" mode="out-in" > 
  at <Index> 
  at <AppMain> 
  at <Index onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > 
  at <RouterView> 
  at <ElConfigProvider locale= {name: 'zh-cn', el: {…}} size="default" > 
  at <App>

问题出在 <transition-group> 组件上尝试传递了一个名为 mode 的非 props 属性,但是该属性无法直接被 <transition-group> 组件继承,因为该组件渲染的是片段或文本根节点。这会导致 Vue 报错。

一、处理方法
1.、移除不必要的属性
   简单的解决方法是移除 <transition-group> 组件上的不必要的 mode 属性。mode 属性是用于设置过渡模式的,但在 Vue 3 中,默认的过渡模式已经改变了。

移除前:

<transition-group name="fade-transform" mode="out-in"></transition-group>

移除后:

<transition-group name="fade-transform"></transition-group>

2、 使用全局配置

Vue 提供了全局配置来设置过渡的模式,可以在 Vue 的创建实例时配置,这样可以避免在每个 <transition-group> 中重复设置。

import { createApp } from 'vue';
import App from './App.vue';
// 预设动画
import animate from './animate';
 
const app = createApp(App);
app.config.globalProperties.animate = animate;
 
app.mount('#app');

然后在组件中,不再需要传递 mode 属性:

<transition-group name="fade-transform"></transition-group>

其中上面的animate.ts文件如下

// 前缀
const animatePrefix = 'animate__animated ';
// 开启随机动画 随机动画值
const animateList: string[] = [
  animatePrefix + 'animate__pulse',
  animatePrefix + 'animate__rubberBand',
  animatePrefix + 'animate__bounceIn',
  animatePrefix + 'animate__bounceInLeft',
  animatePrefix + 'animate__fadeIn',
  animatePrefix + 'animate__fadeInLeft',
  animatePrefix + 'animate__fadeInDown',
  animatePrefix + 'animate__fadeInUp',
  animatePrefix + 'animate__flipInX',
  animatePrefix + 'animate__lightSpeedInLeft',
  animatePrefix + 'animate__rotateInDownLeft',
  animatePrefix + 'animate__rollIn',
  animatePrefix + 'animate__rotateInDownLeft',
  animatePrefix + 'animate__zoomIn',
  animatePrefix + 'animate__zoomInDown',
  animatePrefix + 'animate__slideInLeft',
  animatePrefix + 'animate__lightSpeedIn'
];
// 关闭随机动画后的默认效果
const defaultAnimate = animatePrefix + 'animate__fadeIn';
// 搜索隐藏显示动画
const searchAnimate = {
  enter: '',
  leave: ''
};
// 菜单搜索动画
const menuSearchAnimate = {
  enter: animatePrefix + 'animate__fadeIn',
  leave: animatePrefix + 'animate__fadeOut'
};
// logo动画
const logoAnimate = {
  enter: animatePrefix + 'animate__fadeIn',
  leave: animatePrefix + 'animate__fadeOut'
};
export default {
  animateList,
  defaultAnimate,
  searchAnimate,
  menuSearchAnimate,
  logoAnimate
};

3、 使用 <transition> 组件

如果你只需要简单的过渡效果,可以考虑使用 <transition> 组件而不是 <transition-group>。

<transition name="fade-transform" mode="out-in"></transition>

二.、什么是<TransitionGroup> ?

<TransitionGroup> 是一个内置组件,用于对 v-for 列表中的元素或组件的插入、移除和顺序改变添加动画效果。
<TransitionGroup> 拥有与 <Transition> 除了 mode 以外所有的 props,并增加了两个额外的 props:
interface TransitionGroupProps extends Omit<TransitionProps, 'mode'> {
  /**
   * 如果未定义,则渲染为片段 (fragment)。
   */
  tag?: string
  /**
   * 用于自定义过渡期间被应用的 CSS class。
   * 在模板中使用 kebab-case,例如 move-class="xxx"
   */
  moveClass?: string
}

三、和 <Transition> 的区别

<TransitionGroup> 支持和 <Transition> 基本相同的 props、CSS 过渡 class 和 JavaScript 钩子监听器,但有以下几点区别:

默认情况下,它不会渲染一个容器元素。但你可以通过传入 tag prop 来指定一个元素作为容器元素来渲染。

过渡模式(mode属性)在这里不可用,因为我们不再是在互斥的元素之间进行切换。

列表中的每个元素都必须有一个独一无二的 key属性。

CSS 过渡 class 会被应用在列表内的元素上,而不是容器元素上。

提示:当在 DOM 内模板中使用时,组件名需要写为 <transition-group>。

四.、了解 Vue 3 过渡的改变

Vue 3 中对过渡系统进行了一些改进,包括对默认过渡模式的更改。阅读 Vue 3 的官方文档来了解这些改变,以便更好地使用过渡和动画功能。

五.、查看文档和示例

确保你的代码与 Vue 3 的最新文档和示例保持一致,这有助于避免一些常见的问题并获得正确的用法示例。

在解决这个问题时,应该注意 Vue 的版本和相关文档,确保你所使用的方法与你的 Vue 版本相匹配。

六、总结

错误信息表明 <transition-group> 组件不能接受非 props 属性,特别是对于不被组件直接继承的属性。通过移除不必要的属性、全局配置、使用 <transition> 组件等方法,可以解决这个问题。同时,确保你对 Vue 3 的过渡系统有基本的了解,并保持代码与官方文档和示例保持一致。


相关文章
|
1天前
vue3【实战】语义化首页布局
vue3【实战】语义化首页布局
7 2
|
1天前
|
存储 容器
vue3【实战】来回拖拽放置图片
vue3【实战】来回拖拽放置图片
7 2
|
1天前
|
JavaScript 开发工具 开发者
vue3【提效】使用 VueUse 高效开发(工具库 @vueuse/core + 新增的组件库 @vueuse/components)
vue3【提效】使用 VueUse 高效开发(工具库 @vueuse/core + 新增的组件库 @vueuse/components)
8 1
|
1天前
|
API
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
Pinia 实用教程【Vue3 状态管理】状态持久化 pinia-plugin-persistedstate,异步Action,storeToRefs(),修改State的 $patch,$reset
9 1
|
1天前
|
JavaScript
vue3 【提效】自动注册组件 unplugin-vue-components 实用教程
vue3 【提效】自动注册组件 unplugin-vue-components 实用教程
5 1
|
移动开发 JavaScript 小程序
Vue3 与 Vue2 的Props、全局组件的异同点
Vue3 Props Props 是任何现代 JS 框架的重要组成部分。在组件之间传递数据的能力是Vue项目的基本要素。 Vue3 中,在组件中访问Props的方式与 Vue2 会有所不同。
325 0
Vue3 与 Vue2 的Props、全局组件的异同点
|
1天前
|
数据采集 JavaScript 前端开发
Vue框架的优缺点是什么
【7月更文挑战第5天】 Vue框架:组件化开发利于重用与扩展,响应式数据绑定简化状态管理;学习曲线平缓,生态系统丰富,集成便捷,且具性能优化手段。缺点包括社区规模相对小,类型支持不足(Vue 3.x改善),路由和状态管理需额外配置,SEO支持有限。随着发展,部分缺点正被克服。
7 1
|
1天前
|
JavaScript
Vue卸载eslint的写法,单独安装eslint,单独卸载eslint
Vue卸载eslint的写法,单独安装eslint,单独卸载eslint
|
1天前
|
JavaScript
青戈大佬安装Vue,无Eslint安装版,vue2安装,vue2无eslint,最简单配置Vue安装资料
青戈大佬安装Vue,无Eslint安装版,vue2安装,vue2无eslint,最简单配置Vue安装资料
|
1天前
|
JavaScript
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run
This dependency was not found:* vue/types/umd in ./src/router/index.jsTo install it, you can run