分析vban的utlis中的helper方法(1)——数组
方法
interface TreeHelperConfig {id: string; //用于标识每个节点的唯一标识符字段名称。children: string; // 用于表示子节点的字段名称。pid: string; //用于表示父节点的字段名称。
}// 默认配置
const DEFAULT_CONFIG: TreeHelperConfig = {id: 'id',children: 'children',pid: 'pid',
};// 获取配置。 Object.assign 从一个或多个源对象复制到目标对象
const getConfig = (config: Partial<TreeHelperConfig>) => Object.assign({}, DEFAULT_CONFIG, config);
示例用法
const customConfig = {id: 'nodeId',children: 'subNodes',
};const finalConfig = getConfig(customConfig);
console.log(finalConfig);
输出结果
{
“id”: “nodeId”,
“children”: “subNodes”,
“pid”: “pid”
}
注意事项
只要你的项目使用了 TypeScript,就可以直接使用 Partial,而无需额外的依赖或安装步骤