当前位置:首页 > VUE

vue实现级联

2026-02-10 01:21:43VUE

Vue 实现级联选择器的方法

使用 Element UI 的 Cascader 组件

Element UI 提供了现成的级联选择器组件,适合快速集成到项目中。安装 Element UI 后,直接使用 el-cascader 组件即可。

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>

使用 Ant Design Vue 的 Cascader 组件

Ant Design Vue 也提供了级联选择器组件,适用于遵循 Ant Design 规范的项目。

vue实现级联

<template>
  <a-cascader
    v-model:value="selectedOptions"
    :options="options"
    @change="handleChange"
  />
</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>

手动实现级联选择器

如果需要完全自定义的级联选择器,可以通过动态加载数据的方式实现。

<template>
  <div>
    <select v-model="selectedProvince" @change="loadCities">
      <option v-for="province in provinces" :value="province.id" :key="province.id">
        {{ province.name }}
      </option>
    </select>
    <select v-model="selectedCity" @change="loadDistricts" :disabled="!selectedProvince">
      <option v-for="city in cities" :value="city.id" :key="city.id">
        {{ city.name }}
      </option>
    </select>
    <select v-model="selectedDistrict" :disabled="!selectedCity">
      <option v-for="district in districts" :value="district.id" :key="district.id">
        {{ district.name }}
      </option>
    </select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selectedProvince: '',
      selectedCity: '',
      selectedDistrict: '',
      provinces: [
        { id: '1', name: '浙江省' }
      ],
      cities: [],
      districts: []
    };
  },
  methods: {
    loadCities() {
      this.cities = [
        { id: '101', name: '杭州市' }
      ];
      this.selectedCity = '';
      this.districts = [];
    },
    loadDistricts() {
      this.districts = [
        { id: '1001', name: '西湖区' }
      ];
    }
  }
};
</script>

动态加载数据

对于大数据量的级联选择,可以采用动态加载数据的方式,避免一次性加载所有数据。

<template>
  <el-cascader
    v-model="selectedOptions"
    :props="props"
  ></el-cascader>
</template>

<script>
export default {
  data() {
    return {
      selectedOptions: [],
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          setTimeout(() => {
            const nodes = Array.from({ length: level + 1 }).map((_, i) => ({
              value: `${level}-${i}`,
              label: `选项 ${level}-${i}`,
              leaf: level >= 2
            }));
            resolve(nodes);
          }, 1000);
        }
      }
    };
  }
};
</script>

注意事项

  • 数据格式需符合组件要求,通常为嵌套的 children 结构。
  • 动态加载时需注意性能优化,避免频繁请求。
  • 级联选择器的样式可以通过 CSS 自定义,但需遵循组件库的样式规范。

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

相关文章

vue  select实现

vue select实现

Vue Select 实现方法 在 Vue 中实现下拉选择功能可以使用原生 <select> 标签或第三方库如 vue-select。以下是两种方法的详细说明: 原生 HTML Sele…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <tem…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…