1. 程式人生 > >Vue學習之路(八) --- vue-router使用

Vue學習之路(八) --- vue-router使用

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檔案裡隨意配置你的路由了