這篇文章主要為大家展示了“如何使用Vue-Router2實(shí)現(xiàn)路由功能”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何使用Vue-Router2實(shí)現(xiàn)路由功能”這篇文章吧。

創(chuàng)新互聯(lián)建站主營(yíng)潛江網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā)公司,潛江h(huán)5小程序開(kāi)發(fā)搭建,潛江網(wǎng)站營(yíng)銷(xiāo)推廣歡迎潛江等地區(qū)企業(yè)咨詢(xún)
vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,適合用于構(gòu)建單頁(yè)面應(yīng)用。vue的單頁(yè)面應(yīng)用是基于路由和組件的,路由用于設(shè)定訪問(wèn)路徑,并將路徑和組件映射起來(lái)。傳統(tǒng)的頁(yè)面應(yīng)用,是用一些超鏈接來(lái)實(shí)現(xiàn)頁(yè)面切換和跳轉(zhuǎn)的。在vue-router單頁(yè)面應(yīng)用中,則是路徑之間的切換,也就是組件的切換。
注意: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 1中,使用的是
在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')以上是“如何使用Vue-Router2實(shí)現(xiàn)路由功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:如何使用Vue-Router2實(shí)現(xiàn)路由功能
分享鏈接:http://chinadenli.net/article10/jgpdgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、品牌網(wǎng)站建設(shè)、App設(shè)計(jì)、用戶(hù)體驗(yàn)、小程序開(kāi)發(fā)、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)