简体   繁体   English

在单独的文件中定义路由并在 index.js 中使用它们

[英]Defining routes in a separate file and using them inside index.js

I have this routes inside my index.js我的index.js中有这条路线

import {
    createRouter,
    createWebHistory
}
from '@ionic/vue-router';
import {
    RouteRecordRaw
}
from 'vue-router';

const routes = [{
        path: '',
        redirect: '/tabs'
    }, {
        path: '/tabs',
        component: () => import('../views/tabs/TabRoot.vue'),
        children: [{
                path: '',
                redirect: '/tabs/home'
            }, {
                path: '/tabs/home',
                component: () => import('../views/tabs/Home.vue')
            }, 
        ]
    },


   //Sell
   
    {
        path: '/sell',
        component: () => import('../views/pages/sell/Sell.vue')
    },


    //Categories
    
    {
        path: '/trending',
        component: () => import('../views/pages/Trending.vue')
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

export default router

and i would like to define the follwoing routes isnide others.js file and have this inside我想在others.js文件中定义以下路由并将其放入其中

{
    path: '/crud_ui_landing_vehicles',
    component: () => import('../views/pages/sell/categories/Vehicles.vue')
},

{
    path: '/crud_ui_landing_properties',
    component: () => import('../views/pages/sell/categories/Properties.vue')
},

The imported routes should be used inside const routes array object.导入的路由应该在const routes数组对象中使用。 How can i define and import the routes file?我如何定义和导入路由文件?

Using the spread operator使用扩展运算符

// others.js
export const others = [...]
import { others } from './others.js'
const routes = [
  ...others,
  👆
  {
    path: ...,
    component: ...
  },
  ...
]

Just use a separate file, like others.js and define the routes there只需使用一个单独的文件,如others.js并在那里定义路由

const routes = ....
export default routes;

And then imported them in your file然后将它们导入到您的文件中

import routes from './others'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM