【发布时间】:2021-07-12 10:10:19
【问题描述】:
我使用的是 Nuxtjs v.2.15.4,我正在尝试一种页面主题化方法。通过这段代码,我可以用我的主题页面覆盖现有页面:
// nuxt.config.js
router: {
extendRoutes(routes, resolve) {
if(process.env.THEME === "mainTheme" && process.env.THEME_CUSTOMIZE === "false"){
return
}
routes.map(route => {
const path = resolve(`src/themes/${process.env.THEME}/${route.chunkName}.vue`)
if (fs.existsSync(path)) {
route.component = path
}
if(process.env.THEME_CUSTOMIZE === "true"){
const pathCustom = resolve(`src/themes/customs/${route.chunkName}.vue`)
if (fs.existsSync(pathCustom)) {
route.component = pathCustom
}
}
return route
})
}
},
我知道我在哪里检查现有路径,如果它不存在,必须推送到路由器。但是什么是可以做命名和其他事情的代码,比如 nuxt 本身? 这是nuxt doc提供的推送到路由器的代码:
router: {
extendRoutes(routes, resolve) {
routes.push({
name: 'custom',
path: '*',
component: resolve(__dirname, 'pages/404.vue')
})
}
}
component 将是 resolve(src/themes/${process.env.THEME}/${route.chunkName}.vue) 就像覆盖代码一样,但是 name 和 path 呢??特别是当页面是动态的!
更新
好的,我认为这种方法有问题。它将检查路线,如果主题文件夹中有相同的路线,它将覆盖。但不会检查主题的页面目录本身是否存在主页面目录中不存在的其他页面!所以我必须如何检查主题的页面目录并通过主题映射并覆盖添加新的主要内容。怎么样!??
【问题讨论】:
标签: vuejs2 nuxt.js vue-router