vue路由配置:
1淀零、安裝
npm install vue-router --save / cnpm install vue-router --save
2、 引入并使用路由 Vue.use(VueRouter) (main.js)
import VueRouter from 'vue-router'
Vue.use(VueRouter)
3儡湾、配置路由
1担巩、創(chuàng)建組件 引入組件
2榨馁、定義路由 (建議復(fù)制s)
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar },
{ path: '*', redirect: '/home' } /*默認(rèn)跳轉(zhuǎn)路由*/
]
3森篷、實(shí)例化VueRouter
const router = new VueRouter({
routes // (縮寫(xiě))相當(dāng)于 routes: routes
})
4、掛載
new Vue({
el: '#app',
router沸停,
render: h => h(App)
})
5 膜毁、根組件的模板里面放上這句話(huà)
<router-view></router-view>
6、路由跳轉(zhuǎn)
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>