当前位置:首页 > VUE

vue排他思想实现

2026-03-28 04:16:25VUE

vue排他思想实现

vue排他思想实现

Vue 排他思想实现方法

排他思想的核心是确保同一时间只有一个元素被激活或选中。Vue 中可以通过多种方式实现这种逻辑。

方法一:使用 v-forv-if 结合

<template>
  <div>
    <button 
      v-for="(item, index) in items" 
      :key="index"
      @click="selectItem(index)"
      :class="{ active: selectedIndex === index }"
    >
      {{ item }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['选项1', '选项2', '选项3'],
      selectedIndex: -1
    }
  },
  methods: {
    selectItem(index) {
      this.selectedIndex = index
    }
  }
}
</script>

<style>
.active {
  background-color: #42b983;
  color: white;
}
</style>

方法二:使用计算属性

<template>
  <div>
    <button 
      v-for="(item, index) in items" 
      :key="index"
      @click="selectedItem = item"
      :class="{ active: isActive(item) }"
    >
      {{ item }}
    </button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['选项1', '选项2', '选项3'],
      selectedItem: null
    }
  },
  computed: {
    isActive() {
      return (item) => item === this.selectedItem
    }
  }
}
</script>

方法三:使用 Vuex 状态管理

// store.js
export default new Vuex.Store({
  state: {
    selectedItem: null
  },
  mutations: {
    setSelectedItem(state, item) {
      state.selectedItem = item
    }
  }
})
<template>
  <div>
    <button 
      v-for="(item, index) in items" 
      :key="index"
      @click="$store.commit('setSelectedItem', item)"
      :class="{ active: $store.state.selectedItem === item }"
    >
      {{ item }}
    </button>
  </div>
</template>

方法四:使用自定义指令

// main.js
Vue.directive('exclusive', {
  bind(el, binding, vnode) {
    el.addEventListener('click', () => {
      const group = binding.arg || 'default'
      const value = binding.value
      vnode.context.$exclusive[group] = value
    })
  },
  update(el, binding, vnode) {
    const group = binding.arg || 'default'
    const value = binding.value
    el.classList.toggle('active', vnode.context.$exclusive[group] === value)
  }
})

Vue.prototype.$exclusive = {}
<template>
  <div>
    <button 
      v-exclusive:group1="'option1'"
      @click="$exclusive.group1 = 'option1'"
    >选项1</button>
    <button 
      v-exclusive:group1="'option2'"
      @click="$exclusive.group1 = 'option2'"
    >选项2</button>
  </div>
</template>

方法五:使用事件总线

// eventBus.js
import Vue from 'vue'
export const EventBus = new Vue()
<template>
  <div>
    <button 
      v-for="(item, index) in items" 
      :key="index"
      @click="emitSelection(item)"
      :class="{ active: selectedItem === item }"
    >
      {{ item }}
    </button>
  </div>
</template>

<script>
import { EventBus } from './eventBus'
export default {
  data() {
    return {
      items: ['选项1', '选项2', '选项3'],
      selectedItem: null
    }
  },
  created() {
    EventBus.$on('item-selected', (item) => {
      this.selectedItem = item
    })
  },
  methods: {
    emitSelection(item) {
      EventBus.$emit('item-selected', item)
    }
  }
}
</script>

这些方法各有优缺点,可以根据项目复杂度和需求选择最适合的实现方式。简单场景推荐使用方法一或方法二,大型项目推荐使用方法三或方法五。

标签: 思想vue
分享给朋友:

相关文章

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue ref实现

vue ref实现

Vue 中 ref 的实现 在 Vue 中,ref 是一个用于创建响应式引用的 API,适用于基本类型和对象。以下是 ref 的核心实现原理和使用方法。 基本实现原理 ref 通过 Reactive…

vue实现treetable

vue实现treetable

Vue实现TreeTable的方法 使用第三方组件库(如Element UI) Element UI的el-table组件支持树形表格展示,通过设置row-key和tree-props属性即可实现。…

vue实现dialog

vue实现dialog

Vue 实现 Dialog 的方法 使用 Vue 原生组件实现 Vue 可以通过组件化的方式实现 Dialog,以下是一个简单的实现示例: <template> <div>…

vue实现颜色闪烁

vue实现颜色闪烁

实现颜色闪烁的方法 在Vue中实现颜色闪烁效果可以通过CSS动画或JavaScript定时器动态修改样式。以下是两种常见的实现方式: 使用CSS动画实现 通过定义@keyframes动画规则,结合V…