当前位置:首页 > VUE

vue实现列表联动插件

2026-02-23 11:48:21VUE

Vue 列表联动插件实现方案

基于 v-model 的双向绑定

通过 Vue 的 v-model 实现父子组件数据同步,适用于简单联动场景。

<template>
  <div>
    <parent-list v-model="selectedItems" :items="parentItems" />
    <child-list :items="filteredChildItems" />
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedItems: [],
      parentItems: [...],
      allChildItems: [...]
    }
  },
  computed: {
    filteredChildItems() {
      return this.allChildItems.filter(item => 
        this.selectedItems.includes(item.parentId)
      )
    }
  }
}
</script>

使用事件总线通信

通过 Vue 的 EventBus 实现跨组件通信,适合复杂层级结构。

// event-bus.js
import Vue from 'vue'
export const EventBus = new Vue()

// ParentComponent.vue
EventBus.$emit('parent-selected', selectedIds)

// ChildComponent.vue
EventBus.$on('parent-selected', ids => {
  this.filteredItems = this.allItems.filter(item => ids.includes(item.parentId))
})

封装可复用插件

创建独立插件实现标准化列表联动功能。

vue实现列表联动插件

// list-linkage.js
const ListLinkage = {
  install(Vue) {
    Vue.mixin({
      methods: {
        $updateLinkedList(sourceKey, targetKey, filterFn) {
          this.$watch(sourceKey, newVal => {
            this[targetKey] = this.allItems.filter(item => filterFn(item, newVal))
          }, { deep: true })
        }
      }
    })
  }
}

// main.js
import ListLinkage from './list-linkage'
Vue.use(ListLinkage)

// 组件内使用
this.$updateLinkedList('selectedParents', 'filteredChildren', (child, parents) => {
  return parents.some(p => p.id === child.parentId)
})

基于 Provide/Inject 的层级传递

适合深层嵌套组件结构,通过依赖注入实现数据共享。

// 祖先组件
export default {
  provide() {
    return {
      listLinkage: {
        selected: Vue.observable([]),
        update: this.updateSelection
      }
    }
  }
}

// 后代组件
export default {
  inject: ['listLinkage'],
  methods: {
    handleSelect(item) {
      this.listLinkage.update(item.id)
    }
  }
}

性能优化建议

对于大数据量场景应添加防抖处理和虚拟滚动。

vue实现列表联动插件

import debounce from 'lodash/debounce'

export default {
  methods: {
    updateFilter: debounce(function(searchTerm) {
      this.filtered = this.items.filter(i => 
        i.name.includes(searchTerm)
      )
    }, 300)
  }
}

完整插件示例

以下是封装成 npm 包的完整插件结构示例:

// index.js
export default {
  install(Vue, options = {}) {
    const defaultOptions = {
      debounceTime: 200,
      linkageKey: 'linkageId'
    }

    const config = { ...defaultOptions, ...options }

    Vue.directive('list-linkage', {
      bind(el, binding, vnode) {
        const source = binding.value.source
        const target = binding.value.target
        const filterFn = binding.value.filter || defaultFilter

        vnode.context.$watch(
          source, 
          debounce(newVal => {
            vnode.context[target] = vnode.context.allItems
              .filter(item => filterFn(item, newVal))
          }, config.debounceTime),
          { deep: true }
        )
      }
    })

    function defaultFilter(item, sourceVal) {
      return sourceVal.includes(item[config.linkageKey])
    }
  }
}

使用时可全局注册或按需引入,通过指令方式实现列表联动:

<template>
  <div v-list-linkage="{ source: 'selected', target: 'filtered' }">
    <!-- 联动列表内容 -->
  </div>
</template>

标签: 插件列表
分享给朋友:

相关文章

vue实现功能插件

vue实现功能插件

Vue 插件实现方法 Vue 插件通常用于封装全局功能(如指令、过滤器、混入等),以下是实现 Vue 插件的典型方式: 插件基本结构 const MyPlugin = { instal…

vue实现上移下移插件

vue实现上移下移插件

实现思路 在Vue中实现上移下移功能,可以通过操作数组元素的顺序来完成。通常需要绑定点击事件,利用数组的splice方法交换元素位置。 基础实现代码 <template> <d…

vue实现卡片列表

vue实现卡片列表

Vue 实现卡片列表 使用 v-for 动态渲染卡片列表 通过 Vue 的 v-for 指令可以动态渲染卡片列表,数据通常存储在组件的 data 或通过 API 获取。 <template&g…

vue实现索引列表

vue实现索引列表

Vue 实现索引列表 使用第三方库(如 better-scroll 或 vue-index-list) 安装 better-scroll 或 vue-index-list 库,可以快速实现带索引的列表…

vue实现拖拽插件

vue实现拖拽插件

Vue 拖拽插件实现方法 使用原生 HTML5 拖拽 API HTML5 提供了原生的拖拽 API,可以直接在 Vue 中使用。以下是一个简单的示例: <template> <…

vue实现横向列表

vue实现横向列表

实现横向列表的方法 在Vue中实现横向列表可以通过CSS样式控制列表项的排列方式。横向列表通常用于导航菜单、图片展示等场景。 使用flex布局实现 flex布局是实现横向列表最简单的方法之一,通过设…