级连选择器实现懒加载主要是需要在props配置lazyLoad方法,官网并没有通过调接口实现懒加载的具体的例子,只有一个写死的数据。
不过需要注意的是在data里面调用接口的时候需要先保存this变量,否则获取不到methods里面的方法会报错,
lazyLoad方法里面主要是将接口获取到的数据整理成定义的参数
props里面可以针对数组的key,value进行自定义。
其他部分可参考官网。
以下代码供参考:
<el-cascader
v-model="form.projectType"
:options="workTypeOptions"
:props="workTypeOptionsProps"
@change="handleChange"
clearable
></el-cascader>
data() {
let _this = this;
return {
workTypeOptionsProps: {
value: "dictCode",
\nlabel: "dictCodeDesc",
\nlazy: true,
\nemitPath: false,
\nlazyLoad(node, resolve) {
\nconst { level } = node;
\nnode.value &&
\nsetTimeout(async () => {
\nlet nodes = await _this.getProjectTypeOptions(node.value);
\nlet n = nodes.map((item) => ({
\ndictCode: item.project_type,
\ndictCodeDesc: item.project_type_name,
\nleaf: level < 2,
\n}));
\n// 通过调用resolve将子节点数据返回,通知组件数据加载完成
\nresolve(n);
\n}, 1000);
\n},
\n},
\n}
\n}
}
}
methods: {
handleChange(node) {},
async getProjectTypeOptions(workType) {
let res = await getProjectTypeList({ workType: workType });
if (res) {
return res.content || [];
}
},
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
}