以下主要为简单示例,仅供参考,欢迎提出更合理的优化方案;请多指教
const config = {
roo: '/',
user: {
root: '/user',
update: '/update',
pay: {
root: '/pay',
afterPay: '/after',
beforePay: '/before'
}
}
};
const formatRouterConfig = () => _formatRouterConfig(config, '');
const _formatRouterConfig = (routerConfig, baseStr) => {
const newBaseStr = baseStr + (routerConfig.root === undefined ? '' : routerConfig.root);
for (const prop in routerConfig) {
const value = routerConfig[prop];
if (typeof value === 'string') {
if (prop === 'root') {
routerConfig[prop] = baseStr + value;
}else {
routerConfig[prop] = newBaseStr + value;
}
} else {
_formatRouterConfig(routerConfig[prop], newBaseStr)
}
}
}
formatRouterConfig();
console.log(config);
export default config;
备注:也在摸索学习中,很高兴与您共同进步