Vue-router

跳转方式

第一种:普通跳转




第二种:代码跳转




都要定义路由文件:

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    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: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})

export default router

动态路由

 {
    path: '/user/:id',
    name: 'User',
    component: () => import('../views/User.vue')
  }
// User组件




// App.vue



路由懒加载

vue-cli4脚手架的代码已经有懒加载路由

  {
    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: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },

子路由

  {
    path: '/user/:id',
    name: 'User',
    component: () => import('../views/User.vue'),
    children: [
      {
        path: '/user/:id/content',
        component: () => import('../views/User_content.vue')
      }
    ]
  }

你可能感兴趣的:(Vue-router)