1.安装vue-routercnpm
cnpm install vue-router --save
2.引入并Vue.use(VueRouter) 在main.js中引入
import VueRouter from ‘vue-router’
Vue.use(VueRouter)
3.路由配置
a.创建组件,引入组件
b.定义路由
const routes = [
{ path: ‘/foo’, component: Foo },
{ path: ‘/bar’, component: Bar },
{ path: ‘*’, redirect: ‘/home’ } //默认跳转路由
]
4.实列化VueRouter
const router = new VueRouter({
routes // (缩写) 相当于 routes: routes
})
5. 创建和挂载根实例。
记得要通过 router 配置参数注入路由,
从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount(’#app’)
// 5.放在App.vue中