Vue.js:使用Vue-Router 2實(shí)現(xiàn)路由功能介紹
注意: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)來
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í)例。通過 router 配置參數(shù)注入路由,從而讓整個(gè)應(yīng)用都有路由功能
const app = new Vue({
router,
render: h => h(App)
}).$mount('#app')
經(jīng)過上面的配置之后呢,路由匹配到的組件將會(huì)渲染到App.vue里的<router-view></router-view>
那么這個(gè)App.vue里應(yīng)該這樣寫:
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
index.html里呢要這樣寫:
<body> <div id="app"></div> </body>
這樣就會(huì)把渲染出來的頁面掛載到這個(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}
] }
]
通過/index/info就可以訪問到info組件了
四、懶加載
const routes = [
{ path: '/index', component: resolve => require(['./index.vue'], resolve) },
{ path: '/hello', component: resolve => require(['./hello.vue'], resolve) },
]
通過懶加載就不會(huì)一次性把所有組件都加載進(jìn)來,而是當(dāng)你訪問到那個(gè)組件的時(shí)候才會(huì)加載那一個(gè)。對(duì)于組件比較多的應(yīng)用會(huì)提高首次加載速度。
五、<router-link>
在vue-router 1中,使用的是<a v-link="{path:'/index'}"></a>
在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>
<!-- 不寫 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>
<!-- 帶查詢參數(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)片段 和 全匹配片段,如果沒有路由參數(shù),就是一個(gè)空對(duì)象。
3.$route.query
一個(gè) key/value 對(duì)象,表示 URL 查詢參數(shù)。例如,對(duì)于路徑 /foo?user=1,則有 $route.query.user == 1,如果沒有查詢參數(shù),則是個(gè)空對(duì)象。
4.$route.hash
當(dāng)前路由的 hash 值 (不帶 #) ,如果沒有 hash 值,則為空字符串。
5.$route.fullPath
完成解析后的 URL,包含查詢參數(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')
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
proxy代理不生效以及vue?config.js不生效解決方法
在開發(fā)Vue項(xiàng)目過程中,使用了Proxy代理進(jìn)行數(shù)據(jù)劫持,但是在實(shí)際運(yùn)行過程中發(fā)現(xiàn)代理并沒有生效,也就是說數(shù)據(jù)并沒有被劫持,這篇文章主要給大家介紹了關(guān)于proxy代理不生效以及vue?config.js不生效解決方法的相關(guān)資料,需要的朋友可以參考下2023-11-11
Vue?+?ElementUI表格內(nèi)實(shí)現(xiàn)圖片點(diǎn)擊放大效果的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Vue?+?ElementUI表格內(nèi)實(shí)現(xiàn)圖片點(diǎn)擊放大效果的兩種實(shí)現(xiàn)方式,第一種使用el-popover彈出框來實(shí)現(xiàn)而第二種使用v-viewer插件實(shí)現(xiàn),需要的朋友可以參考下2024-08-08
vue中的rules表單校驗(yàn)規(guī)則使用方法示例詳解 :rules=“rules“
這篇文章主要介紹了vue中的rules表單校驗(yàn)規(guī)則使用方法示例詳解 :rules=“rules“,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
Vue中瀑布流布局與圖片加載優(yōu)化的實(shí)現(xiàn)
本文主要介紹了Vue中瀑布流布局與圖片加載優(yōu)化的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
vue3.2?reactive函數(shù)問題小結(jié)
reactive用來包裝一個(gè)對(duì)象,使其每個(gè)對(duì)象屬性都具有響應(yīng)性(也就是深層次響應(yīng)式),這篇文章主要介紹了vue3.2?reactive函數(shù)注意點(diǎn)及問題小結(jié),需要的朋友可以參考下2022-12-12
深入探索Vue中樣式綁定的七種實(shí)現(xiàn)方法
在?Vue.js?開發(fā)中,合理地控制元素的樣式對(duì)于構(gòu)建高質(zhì)量的用戶界面至關(guān)重要,Vue?提供了靈活的方式來綁定樣式,這篇文章將探索?Vue?中設(shè)置樣式的七種做法,并結(jié)合代碼,逐步說明每種方法的實(shí)現(xiàn),需要的朋友可以參考下2024-03-03

