JavaScript 代码优化:使用 switch 语句简化路由跳转
function navigateToURL(key, url, options) {
switch(key) {
case 'customerVisitNum':
url && uni.navigateTo({
url: ${url}?followupType=3&timeType=${options.timeType}&employeeViewMy=${options.employeeViewMy}
});
break;
case 'technicalExchangeNum':
url && uni.navigateTo({
url: ${url}?followupType=1&timeType=${options.timeType}&employeeViewMy=${options.employeeViewMy}
});
break;
case 'liveDemonstrationNum':
url && uni.navigateTo({
url: ${url}?followupType=2&timeType=${options.timeType}&employeeViewMy=${options.employeeViewMy}
});
break;
case 'workPlanDay':
url && uni.navigateTo({
url: ${url}?activeTabKey=${options.activeTabKey}
});
break;
case 'workPlanWeekly':
url && uni.navigateTo({
url: ${url}?activeTabKey=${options.activeTabKey}
});
break;
case 'curCustomerNum':
url && uni.navigateTo({
url: ${url}?employeeViewMy=${options.employeeViewMy}
});
break;
case 'currentCustomerLink':
url && uni.navigateTo({
url: ${url}?&timeType=${options.timeType}&employeeViewMy=${options.employeeViewMy}
});
break;
default:
url && uni.navigateTo({ url });
break;
}
}
// Call the function with the appropriate parameters navigateToURL('customerVisitNum', url, { timeType, employeeViewMy }); navigateToURL('technicalExchangeNum', url, { timeType, employeeViewMy }); navigateToURL('liveDemonstrationNum', url, { timeType, employeeViewMy }); navigateToURL('workPlanDay', url, { activeTabKey }); navigateToURL('workPlanWeekly', url, { activeTabKey }); navigateToURL('curCustomerNum', url, { employeeViewMy }); navigateToURL('currentCustomerLink', url, { timeType, employeeViewMy }); navigateToURL('someOtherKey', url); // Just navigates to the url without any parameters.
原文地址: https://www.cveoy.top/t/topic/mhEy 著作权归作者所有。请勿转载和采集!