TreeSelect 分类选择

单选模式

  1. <van-tree-select
  2. v-model:active-id="state.activeId"
  3. v-model:main-active-index="state.activeIndex"
  4. :items="items"
  5. />
  1. import { reactive } from 'vue';
  2. export default {
  3. setup() {
  4. const state = reactive({
  5. activeId: 1,
  6. activeIndex: 0,
  7. });
  8. const items = [
  9. {
  10. text: '浙江',
  11. children: [
  12. { text: '杭州', id: 1 },
  13. { text: '温州', id: 2 },
  14. ],
  15. },
  16. {
  17. text: '江苏',
  18. children: [
  19. { text: '南京', id: 5 },
  20. ],
  21. },
  22. ];
  23. return {
  24. state,
  25. items,
  26. };
  27. },
  28. };

多选模式

active-id 为数组格式时,可以选中多个右侧选项。

  1. import { reactive } from 'vue';
  2. setup() {
  3. const state = reactive({
  4. activeId: [1, 2],
  5. activeIndex: 0,
  6. });
  7. const items = [
  8. {
  9. text: '浙江',
  10. children: [
  11. { text: '杭州', id: 1 },
  12. { text: '温州', id: 2 },
  13. ],
  14. },
  15. {
  16. text: '江苏',
  17. children: [
  18. { text: '南京', id: 5 },
  19. { text: '无锡', id: 6 },
  20. ],
  21. },
  22. ];
  23. return {
  24. state,
  25. };
  26. },
  27. };
  1. <van-tree-select
  2. v-model:main-active-index="activeIndex"
  3. height="55vw"
  4. :items="items"
  5. >
  6. <template #content>
  7. <van-image
  8. v-if="activeIndex === 0"
  9. src="https://img.yzcdn.cn/vant/apple-1.jpg"
  10. />
  11. <van-image
  12. v-if="activeIndex === 1"
  13. />
  14. </template>
  15. </van-tree-select>

徽标提示

设置 dot 属性后,会在图标右上角展示一个小红点;设置 badge 属性后,会在图标右上角展示相应的徽标。

  1. <van-tree-select
  2. v-model:main-active-index="activeIndex"
  3. height="55vw"
  4. :items="items"
  5. />
  1. import { ref } from 'vue';
  2. export default {
  3. setup() {
  4. const activeIndex = ref(0);
  5. return {
  6. activeIndex,
  7. items: [
  8. { text: '浙江', children: [], dot: true },
  9. { text: '江苏', children: [], badge: 5 },
  10. ],
  11. };
  12. },

API

Props

Slots

Item 数据结构

组件提供了下列 Less 变量,可用于自定义样式,使用方法请参考主题定制