动态路由配置
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')
}]
})