Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié)
注意:vue-router 2只適用于Vue2.x版本,下面我們是基于vue2.0講的如何使用vue-router 2實(shí)現(xiàn)路由功能。
推薦使用npm安裝。
npm install vue-router
一、使用路由
在main.js中,需要明確安裝路由功能:
import Vue from 'vue' import VueRouter from 'vue-router' import App from './App.vue' Vue.use(VueRouter)
1.定義組件,這里使用從其他文件import進(jìn)來(lái)
import index from './components/index.vue' import hello from './components/hello.vue'
2.定義路由
const routes = [ { path: '/index', component: index }, { path: '/hello', component: hello }, ]
3.創(chuàng)建 router 實(shí)例,然后傳 routes 配置
const router = new VueRouter({ routes })
4.創(chuàng)建和掛載根實(shí)例。通過(guò) router 配置參數(shù)注入路由,從而讓整個(gè)應(yīng)用都有路由功能
const app = new Vue({ router, render: h => h(App) }).$mount('#app')
經(jīng)過(guò)上面的配置之后呢,路由匹配到的組件將會(huì)渲染到App.vue里的<router-view></router-view>
那么這個(gè)App.vue里應(yīng)該這樣寫(xiě):
<template> <div id="app"> <router-view></router-view> </div> </template> index.html里呢要這樣寫(xiě): <body> <div id="app"></div> </body>
這樣就會(huì)把渲染出來(lái)的頁(yè)面掛載到這個(gè)id為app的div里了。
二、重定向 redirect
const routes = [ { path: '/', redirect: '/index'}, // 這樣進(jìn)/ 就會(huì)跳轉(zhuǎn)到/index { path: '/index', component: index } ]
三、嵌套路由
const routes = [ { path: '/index', component: index, children: [ { path: 'info', component: info} ] } ]
通過(guò)/index/info就可以訪問(wèn)到info組件了
四、懶加載
const routes = [ { path: '/index', component: resolve => require(['./index.vue'], resolve) }, { path: '/hello', component: resolve => require(['./hello.vue'], resolve) }, ]
通過(guò)懶加載就不會(huì)一次性把所有組件都加載進(jìn)來(lái),而是當(dāng)你訪問(wèn)到那個(gè)組件的時(shí)候才會(huì)加載那一個(gè)。對(duì)于組件比較多的應(yīng)用會(huì)提高首次加載速度。
五、<router-link>
在vue-router 2中,使用了<router-link></router-link>替換1版本中的a標(biāo)簽
<!-- 字符串 --> <router-link to="home">Home</router-link> <!-- 渲染結(jié)果 --> <a href="home" rel="external nofollow" >Home</a> <!-- 使用 v-bind 的 JS 表達(dá)式 --> <router-link v-bind:to="'home'">Home</router-link> <!-- 不寫(xiě) v-bind 也可以,就像綁定別的屬性一樣 --> <router-link :to="'home'">Home</router-link> <!-- 同上 --> <router-link :to="{ path: 'home' }">Home</router-link> <!-- 命名的路由 --> <router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link> <!-- 帶查詢(xún)參數(shù),下面的結(jié)果為 /register?plan=private --> <router-link :to="{ path: 'register', query: { plan: 'private' }}">Register</router-link>
六、路由信息對(duì)象
1.$route.path
字符串,對(duì)應(yīng)當(dāng)前路由的路徑,總是解析為絕對(duì)路徑,如 "/foo/bar"。
2.$route.params
一個(gè) key/value 對(duì)象,包含了 動(dòng)態(tài)片段 和 全匹配片段,如果沒(méi)有路由參數(shù),就是一個(gè)空對(duì)象。
3.$route.query
一個(gè) key/value 對(duì)象,表示 URL 查詢(xún)參數(shù)。例如,對(duì)于路徑 /foo?user=1,則有 $route.query.user == 1,如果沒(méi)有查詢(xún)參數(shù),則是個(gè)空對(duì)象。
4.$route.hash
當(dāng)前路由的 hash 值 (不帶 #) ,如果沒(méi)有 hash 值,則為空字符串。
5.$route.fullPath
完成解析后的 URL,包含查詢(xún)參數(shù)和 hash 的完整路徑。
6.$route.matched
一個(gè)數(shù)組,包含當(dāng)前路由的所有嵌套路徑片段的 路由記錄 。路由記錄就是 routes 配置數(shù)組中的對(duì)象副本(還有在 children 數(shù)組)。
綜合上述,一個(gè)包含重定向、嵌套路由、懶加載的main.js如下:
import Vue from 'vue' import VueRouter from 'vue-router' import App from './App' Vue.use(VueRouter) const router = new VueRouter({ routes:[ { path: '/', redirect: '/index' }, { path: '/index', component: resolve => require(['./components/index.vue'], resolve), children:[ { path: 'info', component: resolve => require(['./components/info.vue'], resolve) } ] }, { path: '/hello', component: resolve => require(['./components/hello.vue'], resolve) }, ] }) const app = new Vue({ router, render: h => h(App) }).$mount('#app')
更詳細(xì)的vue-router功能請(qǐng)參考文檔:https://router.vuejs.org/zh-cn/
以上這篇Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在Vue3中使用localStorage保存數(shù)據(jù)的流程步驟
在前端開(kāi)發(fā)中,尤其是利用Vue3構(gòu)建現(xiàn)代Web應(yīng)用時(shí),掌握如何使用本地存儲(chǔ)(localStorage)來(lái)保存數(shù)據(jù)是非常重要的能力,在這篇博客中,我將詳細(xì)介紹如何在Vue3中使用localStorage保存數(shù)據(jù),并提供示例代碼來(lái)幫助理解,需要的朋友可以參考下2024-06-06Vue中this.$router.push參數(shù)獲取方法
下面小編就為大家分享一篇Vue中this.$router.push參數(shù)獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Element InfiniteScroll無(wú)限滾動(dòng)的具體使用方法
這篇文章主要介紹了Element InfiniteScroll無(wú)限滾動(dòng)的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Vue input輸入框中的值如何變成黑點(diǎn)問(wèn)題
這篇文章主要介紹了Vue input輸入框中的值如何變成黑點(diǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04解決vue 界面在蘋(píng)果手機(jī)上滑動(dòng)點(diǎn)擊事件等卡頓問(wèn)題
這篇文章主要介紹了vue 界面在蘋(píng)果手機(jī)上滑動(dòng)點(diǎn)擊事件等卡頓解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11Vue 組件組織結(jié)構(gòu)及組件注冊(cè)詳情
這篇文章主要介紹的是Vue 組件組織結(jié)構(gòu)及組件注冊(cè),為了能在模板中使用,這些組件必須先注冊(cè)以便 Vue 能夠識(shí)別。這里有兩種組件的注冊(cè)類(lèi)型:全局注冊(cè)和局部注冊(cè)。至此,我們的組件都只是通過(guò) Vue.component 全局注冊(cè)的,文章學(xué)詳細(xì)內(nèi)容,需要的朋友可以參考一下2021-10-10Vue之解決Echarts組件使用ID不能復(fù)用的問(wèn)題
這篇文章主要介紹了Vue之解決Echarts組件使用ID不能復(fù)用的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03