vue router 配置路由的方法
用 Vue.js + vue-router 創(chuàng)建單頁(yè)應(yīng)用,是非常簡(jiǎn)單的。使用 Vue.js ,我們已經(jīng)可以通過(guò)組合組件來(lái)組成應(yīng)用程序,當(dāng)你要把 vue-router 添加進(jìn)來(lái),我們需要做的是,將組件(components)映射到路由(routes),然后告訴 vue-router 在哪里渲染它們。
路由的基本實(shí)現(xiàn)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> /* 實(shí)現(xiàn)當(dāng)前 路由導(dǎo)航高亮 */ .router-link-exact-active, .router-link-active { color: red; font-size: 30px; } </style> </head> <body> <div id="app"> <!-- 路由的入口,也就是a標(biāo)簽 --> <router-link to="/home">home</router-link> <router-link to="/about">about</router-link> <!-- 指定頁(yè)面中路由的出口,也就是:路由匹配組件將來(lái)展示在頁(yè)面中的位置 --> <router-view></router-view> </div> <script src="./vue.js"></script> <!-- 引入 路由插件 --> <script src="./node_modules/vue-router/dist/vue-router.js"></script> <script> /* 路由的使用步驟: 1 引入 路由插件的js文件 2 創(chuàng)建幾個(gè)組件 3 通過(guò) VueRouter 來(lái)創(chuàng)建一個(gè)路由的實(shí)例,并且在參數(shù)中配置好路由規(guī)則 4 將 路由實(shí)例 與 Vue實(shí)例關(guān)聯(lián)起來(lái),通過(guò) router 屬性 5 在頁(yè)面中使用 router-link 來(lái)定義導(dǎo)航(a標(biāo)簽) 路由路口 6 在頁(yè)面中使用 router-view 來(lái)定義路由出口(路由內(nèi)容展示在頁(yè)面中的位置) */ // Vue中的路由是:哈希值 和 組件的對(duì)應(yīng)關(guān)系 // component 方法能夠返回一個(gè)對(duì)象,用這個(gè)對(duì)象就可以表示當(dāng)前組件 const Home = Vue.component('home', { template: `<h1>這是 Home 組件</h1>` }) const About = Vue.component('about', { template: `<h1>這是 About 組件</h1>` }) // 配置路由規(guī)則 const router = new VueRouter({ // 通過(guò) routes 來(lái)配置路由規(guī)則,值:數(shù)組 routes: [ // 數(shù)組中的每一項(xiàng)表示一個(gè)具體的路由規(guī)則 // path 用來(lái)設(shè)置瀏覽器URL中的哈希值 // componet 屬性用來(lái)設(shè)置哈希值對(duì)應(yīng)的組件 { path: '/home', component: Home }, { path: '/about', component: About }, // redirect 重定向: 讓當(dāng)前匹配的 / ,跳轉(zhuǎn)到 /home 對(duì)應(yīng)的組件中, 也就是默認(rèn)展示: home組件 { path: '/', redirect: '/home' } ] }) var vm = new Vue({ el: '#app', // Vue的配置對(duì)象中有一個(gè)配置項(xiàng)叫做:router // 用來(lái)指定當(dāng)前要使用的路由 // router: router router }) </script> </body> </html>
重定向
解釋?zhuān)簩?/ 重定向到 /home
{ path: '/', redirect: '/home' }
路由導(dǎo)航高亮
說(shuō)明:當(dāng)前匹配的導(dǎo)航鏈接,會(huì)自動(dòng)添加router-link-exact-active router-link-active類(lèi)
路由參數(shù)
- 說(shuō)明:我們經(jīng)常需要把某種模式匹配到的所有路由,全都映射到同一個(gè)組件,此時(shí),可以通過(guò)路由參數(shù)來(lái)處理
- 語(yǔ)法:/user/:id
- 使用:當(dāng)匹配到一個(gè)路由時(shí),參數(shù)值會(huì)被設(shè)置到 this.$route.params
- 其他:可以通過(guò) $route.query 獲取到 URL 中的查詢(xún)參數(shù) 等
// 鏈接: <router-link to="/user/1001">用戶(hù) Jack</router-link> <router-link to="/user/1002">用戶(hù) Rose</router-link> // 路由: { path: '/user/:id', component: User } // User組件: const User = { template: `<div>User {{ $route.params.id }}</div>` }
嵌套路由 - 子路由
- Vue路由是可以嵌套的,即:路由中又包含子路由
- 規(guī)則:父組件中包含 router-view,在路由規(guī)則中使用 children 配置
// 父組件: const User = Vue.component('user', { template: ` <div class="user"> <h2>User Center</h2> <router-link to="/user/profile">個(gè)人資料</router-link> <router-link to="/user/posts">崗位</router-link> <!-- 子路由展示在此處 --> <router-view></router-view> </div> ` }) // 子組件: const UserProfile = { template: '<h3>個(gè)人資料:張三</h3>' } const UserPosts = { template: '<h3>崗位:FE</h3>' } { path: '/user', component: User, // 子路由配置: children: [ { // 當(dāng) /user/profile 匹配成功, // UserProfile 會(huì)被渲染在 User 的 <router-view> 中 path: 'profile', component: UserProfile }, { // 當(dāng) /user/posts 匹配成功 // UserPosts 會(huì)被渲染在 User 的 <router-view> 中 path: 'posts', component: UserPosts } ] }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue3配置router路由并實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)功能
- Vue3路由配置createRouter、createWebHistory、useRouter和useRoute詳解
- Vue3實(shí)戰(zhàn)學(xué)習(xí)配置使用vue?router路由步驟示例
- vue-router如何實(shí)現(xiàn)history模式配置
- Vue-Router的routes配置詳解
- Vue Router history模式的配置方法及其原理
- vue-router的使用方法及含參數(shù)的配置方法
- 使用vue-router為每個(gè)路由配置各自的title
- vue-router+nginx 非根路徑配置方法
- vue中的Router基本配置命令實(shí)例詳解
相關(guān)文章
vue2項(xiàng)目增加eslint配置代碼規(guī)范示例
這篇文章主要為大家介紹了vue2項(xiàng)目增加eslint配置代碼規(guī)范示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08詳解vue項(xiàng)目?jī)?yōu)化之按需加載組件-使用webpack require.ensure
本篇文章主要介紹了詳解vue項(xiàng)目?jī)?yōu)化之按需加載組件-使用webpack require.ensure,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06vue單向以及雙向數(shù)據(jù)綁定方式(v-bind和v-model的使用)
這篇文章主要介紹了vue單向以及雙向數(shù)據(jù)綁定方式(v-bind和v-model的使用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue中使用console.log打印的實(shí)現(xiàn)
這篇文章主要介紹了vue中使用console.log打印的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue3+vite加載本地js/json文件不能使用require淺析
這篇文章主要給大家介紹了關(guān)于vue3+vite加載本地js/json文件不能使用require的相關(guān)資料,require 是屬于 Webpack 的方法,在v3+vite的項(xiàng)目里面并不適用,需要的朋友可以參考下2023-07-07Vue模板語(yǔ)法中數(shù)據(jù)綁定的實(shí)例代碼
這篇文章主要介紹了Vue模板語(yǔ)法中數(shù)據(jù)綁定的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05