一、安裝vue-routerhtml
npm install vue-router yarn add vue-router
二、引入註冊vue-routervue
import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter)
三、連接跳轉vue-router
<router-link to='/home'></router-link> //你能夠在template中使用它實現一個可點擊跳轉到home.vue的 a 標籤 this.$router.push('/about'); //在methods方法中跳轉到about頁面 this.$router.go('-1'); //在js中返回上一個頁面
四、常常用到npm
this.$route.params.name //在js中獲取路由的參數 .router-link-active //當前選中路由的匹配樣式 $route.query //獲取查詢參數 $route.hash //哈希
五、路由配置編程
export default new Router({ routes:[ { //第一層是頂層路由,頂層路由中的router-view中顯示被router-link選中的子路由 path:'/', name:'Home', component:'Home' },{ path:'/user/:id', //www.xxx.com/user/cai name:'user', //:id是動態路徑參數 component:'user', children:[ { path:'userInfo', //www.xxx.com/user/cai/userInfo component:'userInfo' //子路由將渲染到父組件的router-view中 },{ path:'posts', component:'posts' } ] } ] }) Vue.use(Router);
六、路由參數方式變化時,從新發出請求並更新數據後端
//好比:用戶一切換到用戶二, 路由參數改變了,但組件是同一個,會被複用 // 從 /user/cai 切到 /user/wan 在User組件中: //方法1: watch:{ '$route'(to,from){ //作點什麼,好比:更新數據 } } //方法二: beforeRouteUpdate(to,from,next){ //同上 }
七、編程式導航ide
router.push({name:'user',params:{userId:'123'}}) //命名路由導航到user組件 <router-link :to='{name:'user',params:{userId:'123'}}'>用戶</router-link> router.push({path:'register',query:{plan:'cai'}}) //query查詢參數 router.push({path:`/user/${userId}`}) //query router.push(location,onComplete,onAbort) router.replace() //替換 router.go(-1)
八、命名視圖post
//當前組件中只有一個 router-view 時,子組件默認渲染到這裏 <router-view class='default'></router-view> <router-view class='a' name='left'></router-view> <router-view class='b' name='main'></router-view> routes:[ { path:'/', components:{ default:header, left:nav, main:content //content組件會渲染在name爲main的router-view中 } } ] //嵌套命名視圖就是:子路由+命名視圖
九、重定向與別名ui
const router = new VueRouter({ routes: [ { path: '/a', redirect: '/b' }, { path: '/b', redirect: { name: 'foo' }}, //命名路由方式 { path: '/c', redirect: to => { //動態返回重定向目標 // 方法接收 目標路由 做爲參數 // return 重定向的 字符串路徑/路徑對象 }} ] }) const router = new VueRouter({ routes: [ { path: '/a', component: A, alias: '/b' } //別名:當訪問 /b 時也會使用A組件 ] })
十、路由組件傳參this
const User={ props:['id'], template:`<div>{{id}}</div>` } const router = new VueRouter({ routes: [ { path: '/user/:id', component: User, props: true }, // 對於包含命名視圖的路由,你必須分別爲每一個命名視圖添加 `props` 選項: { path: '/user/:id', components: { default: User, sidebar: Sidebar }, props: { default: true, sidebar: false } } ] })
十一、HTML5的History模式下服務端配置
const router = new VueRouter({ mode: 'history', routes: [ { path: '*', component: 404} ] }) 後端配置: //Nginx location / { try_files $uri $uri/ /index.html; } //Apache <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule> //Node.js const http = require('http') const fs = require('fs') const httpPort = 80 http.createServer((req, res) => { fs.readFile('index.htm', 'utf-8', (err, content) => { if (err) { console.log('沒法打開index.htm頁面.') } res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }) res.end(content) }) }).listen(httpPort, () => { console.log('打開: http://localhost:%s', httpPort) }) //使用了Node.js的Express [使用中間件][1]