let settingRenderURL = '/templates/settingRender.html',
addPayRenderURL = '/templates/addPayRender.html',
feedbackRenderURL = '/templates/feedbackRender.html',
privacyPolicyRenderURL = '/templates/privacyPolicyRender.html',
termsRenderURL = '/templates/termsRender.html',
gettingStartRenderURL = '/templates/gettingStartRender.html',
errorRenderURL = '/templates/errorRender.html',
addBookRenderURL = '/templates/addBookRender.html',
faqRenderURL = '/templates/faqRender.html'
// when production
if (location.host.indexOf('127.0.0.1') === -1) {
settingRenderURL = settingRenderURL.replace('.html', '/').replace('/templates', '')
addPayRenderURL = addPayRenderURL.replace('.html', '/').replace('/templates', '')
feedbackRenderURL = feedbackRenderURL.replace('.html', '/').replace('/templates', '')
gettingStartRenderURL = gettingStartRenderURL.replace('.html', '/').replace('/templates', '')
faqRenderURL = faqRenderURL.replace('.html', '/').replace('/templates', '')
privacyPolicyRenderURL = privacyPolicyRenderURL.replace('.html', '/').replace('/templates', '')
termsRenderURL = termsRenderURL.replace('.html', '/').replace('/templates', '')
errorRenderURL = errorRenderURL.replace('.html', '/').replace('/templates', '')
addBookRenderURL = addBookRenderURL.replace('.html', '/').replace('/templates', '')
}
補充
想過用迴圈
只是要怎麼重新定義變數?
謝謝指教
初試
code
const routes = {
settingRenderURL: '/templates/settingRender.html',
addPayRenderURL: '/templates/addPayRender.html',
feedbackRenderURL: '/templates/feedbackRender.html',
privacyPolicyRenderURL: '/templates/privacyPolicyRender.html',
termsRenderURL: '/templates/termsRender.html',
gettingStartRenderURL: '/templates/gettingStartRender.html',
errorRenderURL: '/templates/errorRender.html',
addBookRenderURL: '/templates/addBookRender.html',
faqRenderURL: '/templates/faqRender.html',
};
Object.keys(routes).forEach((key) => {
location.host.indexOf('127.0.0.1') === -1
? routes[key] = routes[key].replace('.html', '/').replace('/templates', '')
: routes[key]
})
用你之前問過的幾個問題來答
如何有效判斷 obj 裡面的 key?
js 把 object value 當新 array 的 key
let obj = {
settingRenderURL: '/templates/settingRender.html',
addPayRenderURL: '/templates/addPayRender.html',
feedbackRenderURL: '/templates/feedbackRender.html',
privacyPolicyRenderURL: '/templates/privacyPolicyRender.html',
termsRenderURL: '/templates/termsRender.html',
gettingStartRenderURL: '/templates/gettingStartRender.html',
errorRenderURL: '/templates/errorRender.html',
addBookRenderURL: '/templates/addBookRender.html',
faqRenderURL: '/templates/faqRender.html'
}
if (location.host.indexOf('127.0.0.1') === -1) {
obj = Object.keys(obj).reduce((item, key) => ({
...item,
[key]: obj[key].replace(/(?:\/templates)\/(\w+)(?:.html)/, '$1/')
}), {});
}
console.log(obj)
/*
{
addBookRenderURL: "addBookRender/",
addPayRenderURL: "addPayRender/",
errorRenderURL: "errorRender/",
faqRenderURL: "faqRender/",
feedbackRenderURL: "feedbackRender/",
gettingStartRenderURL: "gettingStartRender/",
privacyPolicyRenderURL: "privacyPolicyRender/",
settingRenderURL: "settingRender/",
termsRenderURL: "termsRender/"
}
*/