当前位置:首页 > 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>

多级下拉菜单联动

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

vue联动实现代码

<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 实现购票功能 数据结构设计 购票功能通常需要以下数据结构: 场次信息(时间、地点、价格) 座位信息(可选座位、已售座位) 用户订单信息 示例数据结构: data() { return…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…

vue mvvm实现

vue mvvm实现

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

vue实现xterm

vue实现xterm

在 Vue 中集成 Xterm.js Xterm.js 是一个基于 TypeScript 的前端终端组件库,可用于在浏览器中实现终端功能。以下是在 Vue 项目中集成 Xterm.js 的详细步骤。…

vue实现微云

vue实现微云

Vue 实现微云功能 使用 Vue 实现类似微云的网盘功能需要结合前端框架、后端存储及文件处理技术。以下是关键实现步骤和代码示例: 前端框架搭建 使用 Vue CLI 创建项目基础结构,安装必要依赖…

vue实现监控

vue实现监控

Vue 实现监控功能的方法 在 Vue 中实现监控功能通常涉及数据变化监听、事件监听或第三方监控工具集成。以下是几种常见实现方式: 使用 Vue 的 watch 监听数据变化 通过 watch 可以…