当前位置:首页 > VUE

vue实现多箱子

2026-03-28 18:40:15VUE

Vue 实现多箱子功能

多箱子功能通常指在界面中展示多个独立的容器(箱子),每个箱子可以包含不同的内容或数据。以下是几种常见的实现方式:

动态组件实现

使用 Vue 的动态组件特性可以轻松切换不同的箱子内容:

<template>
  <div>
    <button @click="currentBox = 'BoxA'">显示箱子A</button>
    <button @click="currentBox = 'BoxB'">显示箱子B</button>
    <component :is="currentBox" />
  </div>
</template>

<script>
import BoxA from './BoxA.vue'
import BoxB from './BoxB.vue'

export default {
  components: { BoxA, BoxB },
  data() {
    return {
      currentBox: 'BoxA'
    }
  }
}
</script>

v-for 循环渲染多个箱子

当需要同时展示多个相同结构的箱子时,可以使用 v-for:

vue实现多箱子

<template>
  <div>
    <div v-for="(box, index) in boxes" :key="index" class="box">
      <h3>{{ box.title }}</h3>
      <p>{{ box.content }}</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      boxes: [
        { title: '箱子1', content: '内容1' },
        { title: '箱子2', content: '内容2' }
      ]
    }
  }
}
</script>

<style>
.box {
  border: 1px solid #ddd;
  padding: 10px;
  margin: 10px;
}
</style>

使用插槽实现灵活内容

通过插槽可以让箱子组件更灵活地接收不同内容:

<template>
  <div class="box-container">
    <Box v-for="(box, index) in boxList" :key="index" :title="box.title">
      <template #default>
        {{ box.content }}
      </template>
    </Box>
  </div>
</template>

<script>
import Box from './Box.vue'

export default {
  components: { Box },
  data() {
    return {
      boxList: [
        { title: '重要文件', content: '机密文档' },
        { title: '日常用品', content: '文具和工具' }
      ]
    }
  }
}
</script>

Box.vue 组件:

vue实现多箱子

<template>
  <div class="box">
    <h3>{{ title }}</h3>
    <slot></slot>
  </div>
</template>

<script>
export default {
  props: ['title']
}
</script>

使用状态管理(Vuex/Pinia)

当箱子之间需要共享或同步状态时,可以使用状态管理工具:

<template>
  <div>
    <div v-for="box in boxes" :key="box.id">
      <Box :content="box.content" @update="updateBox(box.id, $event)" />
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  computed: {
    ...mapState(['boxes'])
  },
  methods: {
    ...mapMutations(['updateBox'])
  }
}
</script>

箱子拖拽排序实现

如果需要箱子可拖拽排序,可以使用第三方库如 vuedraggable:

<template>
  <draggable v-model="boxes" group="boxes" @end="onDragEnd">
    <div v-for="box in boxes" :key="box.id" class="box">
      {{ box.content }}
    </div>
  </draggable>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  components: { draggable },
  data() {
    return {
      boxes: [
        { id: 1, content: '箱子A' },
        { id: 2, content: '箱子B' }
      ]
    }
  },
  methods: {
    onDragEnd() {
      // 保存新的排序顺序
    }
  }
}
</script>

箱子动画效果

为箱子添加过渡动画:

<template>
  <transition-group name="box-list" tag="div">
    <div v-for="box in boxes" :key="box.id" class="box">
      {{ box.content }}
    </div>
  </transition-group>
</template>

<style>
.box-list-enter-active, .box-list-leave-active {
  transition: all 0.5s;
}
.box-list-enter, .box-list-leave-to {
  opacity: 0;
  transform: translateY(30px);
}
</style>

以上方法可以根据具体需求组合使用,实现各种复杂的多箱子交互场景。

标签: 箱子vue
分享给朋友:

相关文章

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用 n…

vue实现login

vue实现login

Vue 实现登录功能 在 Vue 中实现登录功能通常需要以下几个步骤: 创建登录表单组件 在 Vue 项目中创建一个登录表单组件,包含用户名和密码输入框以及提交按钮。可以使用 Vue 的模板语法和双…

vue实现listview

vue实现listview

Vue 实现 ListView 的方法 基础列表渲染 使用 v-for 指令可以轻松实现基础列表渲染。在 Vue 中,v-for 用于遍历数组或对象,生成动态列表。 <template>…

自己实现vue

自己实现vue

实现简易版 Vue.js 核心功能 要实现一个简易版 Vue.js,需要理解其核心功能:数据响应式、模板编译、依赖收集和虚拟 DOM。以下分模块实现关键功能。 数据响应式(Reactivity) 通…