sourcecode

루트를 다른 파일로 분할

copyscript 2022. 8. 8. 20:01
반응형

루트를 다른 파일로 분할

나는 꽤 큰 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.displaces'?

개별 파일:

import This from '../components/This'
import That from '../components/That'
import ThatOther from '../components/ThatOther'
import ThatOtherOne from '../components/ThatOtherOne'

export default [
  {
        path: '/this', 
        name: 'this', 
        component: This
    },
    {
        path: '/that', 
        name: 'that', 
        component: That
    },
]

루트 파일에서 외부 루트를 Import하고 확산 oeprator를 사용합니다.

import externalRoutesOne from './externalRoutesOne'
import externalRoutesTwo from './externalRoutesTwo'
var router = new Router({                  
routes: [
  ...externalRoutesOne,
  ...externalRoutesTwo
]

주의:...operator는 루트를 정의할 때 필요합니다.

언급URL : https://stackoverflow.com/questions/48264980/splitting-routes-into-separate-files

반응형