当前位置:首页 > VUE

vue实现双联动

2026-03-09 05:52:38VUE

Vue 实现双联动的方法

双联动通常指两个下拉选择框(Select)之间的数据联动,即第一个选择框的值改变时,第二个选择框的选项随之变化。以下是实现双联动的几种方法。

使用 v-model 和 computed 属性

通过 v-model 绑定选择框的值,利用 computed 属性动态计算第二个选择框的选项。

vue实现双联动

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

    <select v-model="selectedCity">
      <option v-for="city in cities" :key="city.id" :value="city.id">
        {{ city.name }}
      </option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      provinces: [
        { id: 1, name: '广东省' },
        { id: 2, name: '江苏省' },
      ],
      cities: [],
      selectedProvince: null,
      selectedCity: null,
    };
  },
  computed: {
    filteredCities() {
      if (!this.selectedProvince) return [];
      return this.cities.filter(city => city.provinceId === this.selectedProvince);
    },
  },
  watch: {
    selectedProvince(newVal) {
      this.cities = this.getCitiesByProvince(newVal);
      this.selectedCity = null;
    },
  },
  methods: {
    getCitiesByProvince(provinceId) {
      // 模拟从接口获取数据
      const cityData = {
        1: [
          { id: 101, name: '广州市', provinceId: 1 },
          { id: 102, name: '深圳市', provinceId: 1 },
        ],
        2: [
          { id: 201, name: '南京市', provinceId: 2 },
          { id: 202, name: '苏州市', provinceId: 2 },
        ],
      };
      return cityData[provinceId] || [];
    },
  },
};
</script>

使用 Vuex 管理状态

如果项目中使用 Vuex,可以将联动数据存储在全局状态中,通过 mapStatemapActions 实现联动。

vue实现双联动

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

    <select v-model="selectedCity">
      <option v-for="city in cities" :key="city.id" :value="city.id">
        {{ city.name }}
      </option>
    </select>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';

export default {
  computed: {
    ...mapState(['provinces', 'cities']),
    selectedProvince: {
      get() {
        return this.$store.state.selectedProvince;
      },
      set(value) {
        this.$store.commit('setSelectedProvince', value);
      },
    },
    selectedCity: {
      get() {
        return this.$store.state.selectedCity;
      },
      set(value) {
        this.$store.commit('setSelectedCity', value);
      },
    },
  },
  methods: {
    ...mapActions(['fetchCitiesByProvince']),
    updateCities() {
      this.fetchCitiesByProvince(this.selectedProvince);
    },
  },
};
</script>

使用第三方组件库

如果使用 Element UI 或 Ant Design Vue 等组件库,可以直接使用其提供的级联选择组件(Cascader)或联动选择功能。

以 Element UI 为例:

<template>
  <div>
    <el-select v-model="selectedProvince" placeholder="请选择省份">
      <el-option
        v-for="province in provinces"
        :key="province.id"
        :label="province.name"
        :value="province.id"
      />
    </el-select>

    <el-select v-model="selectedCity" placeholder="请选择城市">
      <el-option
        v-for="city in cities"
        :key="city.id"
        :label="city.name"
        :value="city.id"
      />
    </el-select>
  </div>
</template>

注意事项

  • 数据初始化时,确保第一个选择框有默认值,避免第二个选择框无数据。
  • 在联动逻辑中,清除第二个选择框的值(如 this.selectedCity = null),避免残留数据。
  • 如果数据来自接口,使用 watch 监听第一个选择框的变化,触发接口请求。

通过以上方法,可以灵活实现 Vue 中的双联动功能。

标签: 双联vue
分享给朋友:

相关文章

vue实现visual

vue实现visual

Vue 实现可视化(Visual)的常见方法 在 Vue 中实现可视化通常涉及数据可视化库的集成,以下是几种主流方案: 使用 ECharts ECharts 是一个强大的 JavaScript 图表…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现erp

vue实现erp

Vue实现ERP系统的关键步骤 技术选型与架构设计 采用Vue 3 + TypeScript构建前端,搭配Pinia状态管理,Element Plus或Ant Design Vue作为UI框架。后端可…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…