vue Router常用屬性詳解
base
基本的路由請求的路徑
如果整個單頁應用服務在 /app/ 下,然后 base 就應該設為 “/app/”,所有的請求都會在url之后加上/app/
new VueRouter({ base: '/app/', ... });
mode
設置路由工作模式hash或history
hash
http:/8000/#/hello
Vue-router 默認使用 hash 模式,使用 hash 模式時 url 中始終有 # 號。不會刷新頁面,也不會發(fā)起新的 HTTP 請求,只是實現客戶端頁面的定位#后面的字符不會被發(fā)送到服務器端,# 可以修改瀏覽器的訪問歷史記錄。hash 模式是通過改變錨點(#)來更新頁面 url,并不會觸發(fā)頁面重新加載。通過window.onhashchange() 監(jiān)聽到hash 的改變,從而處理路由。
history
http:/8000/hello
- Vue-router 默認使用 hash 模式,使用 hash 模式時 url 中始終有 # 號。
- 不會刷新頁面,也不會發(fā)起新的 HTTP 請求,只是實現客戶端頁面的定位
- #后面的字符不會被發(fā)送到服務器端,# 可以修改瀏覽器的訪問歷史記錄。
- hash 模式是通過改變錨點(#)來更新頁面 url,并不會觸發(fā)頁面重新加載。
- 通過window.onhashchange() 監(jiān)聽到hash 的改變,從而處理路由。
history模式下可能會遇到的問題及解決方案
- 使用history模式通常本地調試沒有什么問題,但是一旦發(fā)布到測試或生產環(huán)境,則會出現頁面白屏或者刷新頁面白屏的現象,這種問題的出現是因為前端和服務端沒有做相應的配置
//前端設置 module.exports = { // publicPath默認值是'/',即你的應用是被部署在一個域名的根路徑上 // 設置為'./',可以避免打包后的靜態(tài)頁面空白 // 當在非本地環(huán)境時,這里以項目test為例,即打包后的h5項目部署服務器的test目錄下 // 那么這里就要把publicPath設置為/test/,表示所有的靜態(tài)資源都在/test/里 // 打包部署后,會發(fā)現index.html引用的靜態(tài)資源都添加了路徑/test/ publicPath: process.env.NODE_ENV == 'development' ? './' : '/test/', ...... } ///服務端設置 location /test{ ... try_files $uri $uri/ /test/index.html //location /test表示項目部署在了 /test目錄下,這里要跟vue.config.js里的publicpath的值保持一致。 之所以刷新頁面白屏,其實是因為路由資源不存在 }
routes
屬性設置匹配的路由地址path與路由組件component
new Router({ ... routes: [ { path: '/', component: () => import('.iews/Index'), name: "home", children: [ { path: '/home', name: "home", meta: { title: '管理' }, component: () => import('.src/home/Index') }, ] }, { path: string,//路由路徑 component: Component, // 當前路由匹配時顯示的路由組件 name: string, // 命名路由 redirect: string | Location | Function, // 路由重定向 props: boolean | Object | Function, //路由傳參 alias: string | Array<string>, // 路由別名 children: Array<RouteConfig>, // 嵌套路由 beforeEnter: (to: Route, from: Route, next: Function) => void, //路由守衛(wèi) caseSensitive: boolean, // 匹配規(guī)則是否大小寫敏感?(默認值:false) } } ] })
props配置(最佳方案)
{ // 二級路由 path: 'message', component: Message, children: [ { // 三級路由 name: 'detail', path: 'details/:id/:title/:desc', // 配置占位符 component: Details, props(route){ // router每次調的時候會把 $route 傳進來,你想怎么取就怎么??! return { id: route.params.id, title: route.params.title, desc: route.params.desc } } // es6解構賦值寫法更簡單 //props({query: {id, title, desc}}){ // return {id, title, desc} //} } ] }
scrollBehavior
置路由跳轉時,頁面滾動條的位置
很多情況下,用戶希望查看詳情頁以后,返回列表頁剛剛瀏覽的位置,但由于列表頁組件已經被銷毀,所以我們重新返回到列表頁后頁面會置頂,就需要重新下拉查看列表,這樣就做了很多沒有必要的操作。
new VueRouter({ ... scrollBehavior() { return { x: 0, y: 0 }; }, });
也可以使用如下兩種方案(更推薦使用scrollBehavior方案)
- 使用路由守衛(wèi)),在beforRouterLeave的路由鉤子記錄當前頁面滾動位置
//在頁面離開時記錄滾動位置 beforeRouteLeave (to, from, next) { this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop next() }, //進入該頁面時,用之前保存的滾動位置賦值 beforeRouteEnter (to, from, next) { next(vm => { document.body.scrollTop = vm.scrollTop }) },
使用keep-alive緩存
//App.vue <template> <div id="app"> <keep-alive> <router-view v-if="$route.meta.keepAlive"></router-view> </keep-alive> <router-view v-if="!$route.meta.keepAlive" /> </div> </template> //router.js routes: [ { path: '/', name: 'List', component: () => import('./list.vue'), meta: { keepAlive: true // 需要緩存 } }, { path: '/content/:contentId', name: 'content', component: () => import('./content.vue'), meta: { keepAlive: false // 不需要緩存 } }, ]
到此這篇關于vueRouter常用屬性的文章就介紹到這了,更多相關vueRouter常用屬性內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!