vue3.0路由配置

import {
    
     createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import Home from '../views/Home.vue'
import about from '../views/component/index.vue'
import Login from '../views/Login.vue'
import {
    
    url} from '../common/utils'
import loginIndex from '../views/login/index.vue'
import aboutChildIndex from '../views/about/child/index.vue'
import one from '../views/component/one.vue'
import two from '../views/component/two.vue'
const routes: Array<RouteRecordRaw> = [
  {
    
    
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
    
    
        path: '/',
        component: loginIndex,
        children: [
          {
    
    
            path: '/',
            component: aboutChildIndex
          }
        ]
      }
    ]
  },
  {
    
    
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: about,
    children: [
      {
    
    
        path: '/about', //  其他建议使用 比如 /about 作为嵌套使用 使用/也可
        component: one,
        children: [
          {
    
    
            path: '/about',
            component: two
          }
        ]
      }
    ]
  },
  {
    
    
    path: '/Login',
    name: 'Login',
    component: Login,
  },
  {
    
    
    path: '/formIndex',
    component: url('form/index')
  }
]

const router = createRouter({
    
    
  history: createWebHistory(process.env.BASE_URL),
  routes,
  linkActiveClass: 'active-class', // 自定义class 作为 路由样式
  linkExactActiveClass: 'init-class',
  scrollBehavior (to, from, savedPosition):any {
    
    
    console.log(to, from , savedPosition)
    // return 期望滚动到哪个的位置
    if(savedPosition) {
    
    
      return savedPosition;
    }else{
    
    
      return {
    
    x:0,y:0}
    }
  }
})
router.beforeEach((to,from,next)=>{
    
    
  console.log(to,from)
  next()
})
router.afterEach((to,from)=>{
    
    
  console.log(to,from)
})
router.addRoute({
    
    
  path: '/one',
  component: one
})

export default router

猜你喜欢

转载自blog.csdn.net/qq_43505774/article/details/114296712