van-tree-select 使用
时间: 2023-11-30 09:40:39 浏览: 300
van-tree-select是一个基于Vue.js的树形选择器组件,可以用于分类选择等场景。下面是van-tree-select的使用方法:
1. 引入van-tree-select组件和所需样式:
```html
<template>
<div>
<van-tree-select
:items="items"
:active-id.sync="activeId"
:main-active-index.sync="mainActiveIndex"
@click-nav="onClickNav"
/>
</div>
</template>
<script>
import { TreeSelect } from 'vant';
export default {
components: {
[TreeSelect.name]: TreeSelect,
},
data() {
return {
activeId: 2,
mainActiveIndex: 0,
items: [
{
// 标题
text: '所有城市',
// 禁用选项
disabled: true,
// 当前分组下面的所有选项
children: [
{
// 选项文案
text: '北京市',
// 选项id,选中时传递的值
id: 1,
},
{
text: '上海市',
id: 2,
},
],
},
],
};
},
methods: {
onClickNav({ detail = {} }) {
this.mainActiveIndex = detail.index || 0;
},
},
};
</script>
```
2. 在需要使用van-tree-select的页面中引入组件:
```html
<template>
<div>
<van-tree-select
:items="items"
:active-id.sync="activeId"
:main-active-index.sync="mainActiveIndex"
@click-nav="onClickNav"
/>
</div>
</template>
<script>
import { TreeSelect } from 'vant';
export default {
components: {
[TreeSelect.name]: TreeSelect,
},
data() {
return {
activeId: 2,
mainActiveIndex: 0,
items: [
{
// 标题
text: '所有城市',
// 禁用选项
disabled: true,
// 当前分组下面的所有选项
children: [
{
// 选项文案
text: '北京市',
// 选项id,选中时传递的值
id: 1,
},
{
text: '上海市',
id: 2,
},
],
},
],
};
},
methods: {
onClickNav({ detail = {} }) {
this.mainActiveIndex = detail.index || 0;
},
},
};
</script>
```
3. 在组件中设置所需参数,例如:items、active-id、main-active-index等。
阅读全文
相关推荐

















