溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么Vue.js使用Vue-Router 2實(shí)現(xiàn)路由功能

發(fā)布時間:2021-04-26 13:44:56 來源:億速云 閱讀:208 作者:小新 欄目:web開發(fā)

這篇文章主要介紹怎么Vue.js使用Vue-Router 2實(shí)現(xiàn)路由功能,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

為什么要使用Vue

Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護(hù)性和可測試性更強(qiáng)的代碼庫,Vue允許可以將一個網(wǎng)頁分割成可復(fù)用的組件,每個組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網(wǎng)頁中相應(yīng)的地方,所以越來越多的前端開發(fā)者使用vue。

注意: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ù)注入路由,從而讓整個應(yīng)用都有路由功能
const app = new Vue({
  router,
 render: h => h(App)
}).$mount('#app')

經(jīng)過上面的配置之后呢,路由匹配到的組件將會渲染到App.vue里的<router-view></router-view>

那么這個App.vue里應(yīng)該這樣寫:

<template>
 <div id="app">
      <router-view></router-view>
 </div>
</template>

index.html里呢要這樣寫:

<body>
<div id="app"></div>
</body>

這樣就會把渲染出來的頁面掛載到這個id為app的div里了。

二、重定向  redirect

const routes = [
{ path: '/', redirect: '/index'},   // 這樣進(jìn)/ 就會跳轉(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) },
]

通過懶加載就不會一次性把所有組件都加載進(jìn)來,而是當(dāng)你訪問到那個組件的時候才會加載那一個。對于組件比較多的應(yīng)用會提高首次加載速度。

五、<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>

六、路由信息對象

1.$route.path

字符串,對應(yīng)當(dāng)前路由的路徑,總是解析為絕對路徑,如 "/foo/bar"。

2.$route.params

一個 key/value 對象,包含了 動態(tài)片段 和 全匹配片段,如果沒有路由參數(shù),就是一個空對象。

3.$route.query

一個 key/value 對象,表示 URL 查詢參數(shù)。例如,對于路徑 /foo?user=1,則有 $route.query.user == 1,如果沒有查詢參數(shù),則是個空對象。

4.$route.hash

當(dāng)前路由的 hash 值 (不帶 #) ,如果沒有 hash 值,則為空字符串。

5.$route.fullPath

完成解析后的 URL,包含查詢參數(shù)和 hash 的完整路徑。

6.$route.matched

一個數(shù)組,包含當(dāng)前路由的所有嵌套路徑片段的 路由記錄 。路由記錄就是 routes 配置數(shù)組中的對象副本(還有在 children 數(shù)組)。

綜合上述,一個包含重定向、嵌套路由、懶加載的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.js使用Vue-Router 2實(shí)現(xiàn)路由功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI