vue router 能否自动配置 router对象

Vanghohs
  • 183

比如 router/index.js 中配置router对象:

Vue.use(Router)
export default new Router({
  mode: 'hash', // Demo is living in GitHub.io, so required!
  // history: true,
  // saveScrollPosition: true,
  linkActiveClass: 'open active',
  scrollBehavior: () => ({ y: 0 }),
  routes: [

其中大部分内容为固定格式:

            { path: 'doc01', name: 'doc01', component: require('../urls/edit/Doc01.vue') },
            { path: 'doc02', name: 'doc02', component: require('../urls/edit/Doc02.vue') },
            { path: 'doc03', name: 'doc03', component: require('../urls/edit/Doc03.vue') },
            { path: 'doc04', name: 'doc04', component: require('../urls/edit/Doc04.vue') },
            { path: 'doc05', name: 'doc05', component: require('../urls/edit/Doc05.vue') },
            { path: 'doc06', name: 'doc06', component: require('../urls/edit/Doc06.vue') },
            { path: 'doc07', name: 'doc07', component: require('../urls/edit/Doc07.vue') },
            ........

可否通过规则自动生成上面的代码?

回复
阅读 2k
3 个回答

直接遍历不就行了?

能给个代码参考下吗?

nuxt.js这个就可以自动生成路由,
你也可以自己用node封装一个

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏