Vue Router 的路由配置 动态路由和懒加载

动态路由配置router/index.jsimport Vue from vueimport Router from vue-routerimport Detail from @/pages/detail/DetailVue.use(Router)export default new Router({routes: [{path: /,name: Home,...

动态路由配置

router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import Detail from '@/pages/detail/Detail'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    }, {
      path: '/detail/:id',
      name: 'Detail',
      component: Detail
    }
  ]
})

pages/detail/Detail.vue

//......代码省略
methods: {
    getDetailInfo () {
      axios.get('/api/detail.json', {
        params: {
          id: this.$route.params.id
        }
      }).then(this.handleGetDataSucc)
    }
}
//......代码省略

懒加载

把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件。

const router = new VueRouter({
  routes: [{ 
    path: '/foo', 
    component: () => import('./Foo.vue')
  }]
})

本文标题为:Vue Router 的路由配置 动态路由和懒加载

基础教程推荐