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
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页