【发布时间】:2018-06-24 04:50:06
【问题描述】:
我正在开发一个非常大的 Vue 应用程序,我现在必须在 router/index.js 的一页上写下所有路由,而且它已经变得太长了,以至于无法喜欢甚至维护。 index.js 页面充满了诸如...之类的语句
import This from '../components/This'
import That from '../components/That'
import ThatOther from '../components/ThatOther'
import ThatOtherOne from '../components/ThatOtherOne'
// and so on and so on...then at the bottom
var router = new Router({
routes: [
{
path: '/this',
name: 'this',
component: This
},
{
path: '/that',
name: 'that',
component: That
},
// and so on...so many lines of similar and "repetitive" code
由于我的应用程序可以分组为“模块”,有没有办法将路由拆分为单独的文件(import 语句和路由器条目),如router/this.js,router/that.js...', then add them to the main route page,router/index.js`?
【问题讨论】:
-
routes只是一个路由对象数组。您可以根据需要导出任意数量的路由数组并将它们连接在一起,然后再将它们添加到路由器。 -
谢谢!答案中的例子更好!