elementui 级联选择器 动态加载
<template>
<!-- 商品分类 -->
<div class='ProductClassify'>
{{areaCode}}
<el-cascader v-bind="$attrs"
clearable
placeholder="请选择"
v-model="areaCode"
:props="cascaderProps"></el-cascader>
</div>
</template>
<script>
export default {
name: 'ProductClassify',
components: {},
filters: {},
props: {},
data() {
return {
areaCode: '',
cascaderProps: {
// label: "goodsName",//当返回的数据字段不是label和value可以改成实际的字段比如goodsName
// value: "goodsCode",
// children: "options2"
lazy: true,
lazyLoad: this.lazyLoad,
},
};
},
computed: {},
watch: {},
created() {},
mounted() {},
methods: {
lazyLoad(node, resolve) {
this.getData(node, resolve);
},
async getData(node, resolve) {
let that = this;
let level = node.level;
if (!node.data) {
await this.getClassData().then((data) => {
//接口
const nodes = Array.from(data).map((item) => ({
value: item.Code,
label: `${item.Name}`,
leaf: level >= 1,
}));
// 通过调用resolve将子节点数据返回,通知组件数据加载完成
resolve(nodes);
});
} else {
await this.getClassData(node.data.value).then((data) => {
const nodes = Array.from(data).map((item) => ({
value: item.Code,
label: `${item.Name}`,
leaf: level >= 2,
}));
// 通过调用resolve将子节点数据返回,通知组件数据加载完成
resolve(nodes);
});
}
},
getClassData(PCode = '') {
return new Promise((resolve) => {
fetchData({
API: '/api/Goods/UserGetCategoryByPCode',
hideLoading: true,
para: {
PCode: PCode || '',
},
callback: (data) => {
/*
Code: "001"
Name: "电视影音"
PCode: "0"
*/
resolve(data);
},
});
});
},
},
};
</script>
<style lang='scss' scoped>
</style>
注:以上内容仅用于日常学习