当前位置:首页 > VUE

vue 实现级联

2026-02-10 01:14:55VUE

实现级联选择器的基本思路

在Vue中实现级联选择器通常涉及组件间的数据传递和状态管理。核心是通过父子组件通信或集中式状态管理(如Vuex)来同步各级选项的数据。

使用原生select与v-model

对于简单级联,可通过v-model绑定数据,利用@change事件触发下一级选项的更新:

vue 实现级联

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

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

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

<script>
export default {
  data() {
    return {
      provinces: [],
      cities: [],
      districts: [],
      selectedProvince: null,
      selectedCity: null,
      selectedDistrict: null
    }
  },
  methods: {
    loadCities() {
      this.cities = getCitiesByProvince(this.selectedProvince)
      this.selectedCity = null
      this.districts = []
    },
    loadDistricts() {
      this.districts = getDistrictsByCity(this.selectedCity)
    }
  }
}
</script>

使用第三方组件库

主流UI库如Element UI、Ant Design Vue等提供现成的级联组件:

Element UI示例:

vue 实现级联

<template>
  <el-cascader
    v-model="selectedOptions"
    :options="options"
    @change="handleChange"
  ></el-cascader>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      options: [{
        value: 'zhejiang',
        label: '浙江',
        children: [{
          value: 'hangzhou',
          label: '杭州',
          children: [{
            value: 'xihu',
            label: '西湖'
          }]
        }]
      }]
    }
  },
  methods: {
    handleChange(value) {
      console.log(value)
    }
  }
}
</script>

动态加载数据

对于大数据量场景,可采用异步加载方式:

methods: {
  lazyLoad(node, resolve) {
    const { level } = node
    setTimeout(() => {
      const nodes = Array.from({ length: level + 1 })
        .map(item => ({
          value: ++id,
          label: `选项${id}`,
          leaf: level >= 2
        }))
      resolve(nodes)
    }, 1000)
  }
}

自定义级联组件

如需高度定制,可自行实现递归组件:

<template>
  <div class="cascader">
    <div 
      v-for="(item, index) in currentLevel" 
      :key="index"
      @click="selectItem(item)"
    >
      {{ item.label }}
    </div>
    <cascader-panel 
      v-if="selectedItem && selectedItem.children"
      :data="selectedItem.children"
    />
  </div>
</template>

<script>
export default {
  name: 'CascaderPanel',
  props: ['data'],
  data() {
    return {
      selectedItem: null
    }
  },
  computed: {
    currentLevel() {
      return this.data || []
    }
  },
  methods: {
    selectItem(item) {
      this.selectedItem = item
    }
  }
}
</script>

关键注意事项

  • 数据结构建议采用树形格式,包含valuelabelchildren字段
  • 多级联动时需要清空已选下级选项
  • 异步加载时要处理加载状态和错误情况
  • 移动端需考虑交互体验,可结合弹出层实现

以上方案可根据实际项目需求选择或组合使用,复杂场景建议优先考虑成熟组件库的实现。

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

相关文章

vue实现简单的弹窗

vue实现简单的弹窗

使用 Vue 实现简单弹窗 组件基础结构 创建一个名为 Modal.vue 的组件文件,包含模板、脚本和样式部分: <template> <div class="mod…

vue实现3d宇宙

vue实现3d宇宙

Vue实现3D宇宙效果 使用Three.js库 Three.js是一个强大的JavaScript 3D库,可以轻松集成到Vue项目中。安装Three.js依赖: npm install three…

vue实现xss

vue实现xss

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

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,然…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…