Vue學習之路(八) --- vue-router使用
阿新 • • 發佈:2018-11-12
1.首先安裝vue-router ,在專案的資料夾中使用npm命令即npm install vue-router –save(提示:如果你在vue-cli腳手架工具中已經選擇安裝路由可以不用執行這一步)
2.在實戰中,一般路由放在src/router/index.js中具體操作如下
路由的配置,需要一個例項一個路由物件,然後在路由中配置中陣列,具體如下,其中component元件需要自己import引入
new Router({
linkActiveClass: 'active',
routes: [
{
path: '/goods' ,
name: 'goods',
component: goods
},
{
path: '/comments',
name: 'comments',
component: comments
},
{
path: '/shops',
name: 'shops',
component: shops
}
]
})
3.然後在main.js中引入router資料夾下的index.js檔案。
4.最後一步一定寫,就是在app.vue咧寫路由插座router-view
然後你就可以在router/index.js檔案裡隨意配置你的路由了