当前位置:首页 > VUE

vue联动实现代码

2026-01-23 03:45:48VUE

Vue 联动实现代码

Vue 中实现联动效果通常涉及父子组件通信、状态管理或动态数据绑定。以下是几种常见的联动实现方式:

父子组件通信实现联动

通过 props$emit 实现父子组件数据联动:

<!-- 父组件 Parent.vue -->
<template>
  <div>
    <Child :selected="selectedItem" @update="handleUpdate" />
    <p>当前选择:{{ selectedItem }}</p>
  </div>
</template>

<script>
import Child from './Child.vue'
export default {
  components: { Child },
  data() {
    return {
      selectedItem: ''
    }
  },
  methods: {
    handleUpdate(value) {
      this.selectedItem = value
    }
  }
}
</script>
<!-- 子组件 Child.vue -->
<template>
  <select v-model="internalValue" @change="onChange">
    <option value="A">选项A</option>
    <option value="B">选项B</option>
  </select>
</template>

<script>
export default {
  props: ['selected'],
  data() {
    return {
      internalValue: this.selected
    }
  },
  watch: {
    selected(newVal) {
      this.internalValue = newVal
    }
  },
  methods: {
    onChange() {
      this.$emit('update', this.internalValue)
    }
  }
}
</script>

使用 Vuex 实现全局状态联动

当多个组件需要共享联动状态时,可以使用 Vuex:

// store.js
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    currentSelection: null
  },
  mutations: {
    setSelection(state, payload) {
      state.currentSelection = payload
    }
  }
})
<!-- 组件A -->
<template>
  <select v-model="selection" @change="updateSelection">
    <option value="1">选项1</option>
    <option value="2">选项2</option>
  </select>
</template>

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

export default {
  computed: {
    ...mapState(['currentSelection']),
    selection: {
      get() {
        return this.currentSelection
      },
      set(value) {
        this.setSelection(value)
      }
    }
  },
  methods: {
    ...mapMutations(['setSelection']),
    updateSelection() {
      this.setSelection(this.selection)
    }
  }
}
</script>

动态表单联动实现

实现表单字段间的动态联动:

<template>
  <div>
    <select v-model="form.type">
      <option value="student">学生</option>
      <option value="teacher">教师</option>
    </select>

    <div v-if="form.type === 'student'">
      <input v-model="form.studentId" placeholder="学号">
    </div>

    <div v-else-if="form.type === 'teacher'">
      <input v-model="form.teacherId" placeholder="工号">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        type: '',
        studentId: '',
        teacherId: ''
      }
    }
  }
}
</script>

多级下拉菜单联动

实现省市区三级联动效果:

<template>
  <div>
    <select v-model="selectedProvince" @change="loadCities">
      <option v-for="p in provinces" :value="p.id" :key="p.id">
        {{ p.name }}
      </option>
    </select>

    <select v-model="selectedCity" @change="loadDistricts" :disabled="!selectedProvince">
      <option v-for="c in cities" :value="c.id" :key="c.id">
        {{ c.name }}
      </option>
    </select>

    <select v-model="selectedDistrict" :disabled="!selectedCity">
      <option v-for="d in districts" :value="d.id" :key="d.id">
        {{ d.name }}
      </option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      provinces: [],
      cities: [],
      districts: [],
      selectedProvince: null,
      selectedCity: null,
      selectedDistrict: null
    }
  },
  mounted() {
    this.loadProvinces()
  },
  methods: {
    loadProvinces() {
      // 模拟API请求
      this.provinces = [
        { id: 1, name: '广东省' },
        { id: 2, name: '浙江省' }
      ]
    },
    loadCities() {
      if (!this.selectedProvince) return
      // 模拟根据省份加载城市
      this.cities = [
        { id: 101, name: '广州市' },
        { id: 102, name: '深圳市' }
      ]
      this.selectedCity = null
      this.districts = []
    },
    loadDistricts() {
      if (!this.selectedCity) return
      // 模拟根据城市加载区县
      this.districts = [
        { id: 1001, name: '天河区' },
        { id: 1002, name: '越秀区' }
      ]
    }
  }
}
</script>

这些示例展示了 Vue 中实现联动的不同场景和方法,可以根据具体需求选择合适的实现方式。

vue联动实现代码

标签: 代码vue
分享给朋友:

相关文章

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <tem…

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressorj…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 me…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm insta…

vue实现弹窗可切换

vue实现弹窗可切换

实现弹窗可切换的 Vue 方案 动态组件切换 通过 Vue 的 <component :is="currentComponent"> 动态加载不同弹窗组件,结合 v-if 控制显示状态。…

vue中实现显示和隐藏

vue中实现显示和隐藏

控制显示与隐藏的方法 在Vue中实现元素的显示和隐藏可以通过多种方式完成,以下是常见的几种方法: 1. 使用v-if指令v-if根据条件动态添加或移除DOM元素,适合需要频繁切换的场景。当条件为fa…