【vue2】對路由的理解

2023-2-20    藍(lán)藍(lán)設(shè)計(jì)的小編

目錄

 一、vue路由概念

1.概念:

2.初體驗(yàn):

二、路由基礎(chǔ)

1.路由模式        

2.路由寫法

總結(jié)手動自動配置區(qū)別:

三、路由重定向與二級路

1.路由重定向

2.二級路由

 四、路由跳轉(zhuǎn)傳參

1.聲明式導(dǎo)航:

2.編程式導(dǎo)航


 一、vue路由概念

1.概念:

確保我們在vue中實(shí)現(xiàn)頁面跳轉(zhuǎn)到我們所想的頁面

2.初體驗(yàn):

88f51af6257443c5aeb88fa4b47a805a.gif

 可以看到當(dāng)我們點(diǎn)擊不同的組件的時(shí)候我們實(shí)現(xiàn)了路由的功能:在vue中實(shí)現(xiàn)頁面的跳轉(zhuǎn)

注意看,當(dāng)我點(diǎn)擊的時(shí)候上面地址欄中加載了不同的網(wǎng)頁。下面我們來學(xué)習(xí)下路由的寫法

二、路由基礎(chǔ)

1.路由模式

路由兩種模式

路由書寫寫法:

在index.js中的router對象中書寫


  1. const router = new VueRouter({
  2. mode: 'history',//默認(rèn)是hash模式
  3. })

hash模式:

路由例如: http://localhost:8080/#/home

history模式:

路由例如: http://localhost:8080/home


兩種模式的區(qū)別:

  1. 外觀:hash的url有個(gè)#符號,history沒有,history外觀更好看。
  2. 刷新:hash刷新會加載到地址欄對應(yīng)的頁面,history刷新瀏覽器會重新發(fā)起請求,如果服務(wù)端沒有匹配當(dāng)前的url,就會出現(xiàn)404頁面。
  3. history模式以后上線需要服務(wù)器端支持, 否則找的是文件夾

 2.路由寫法

 起步 | Vue RouterVue Router3官網(wǎng)介紹: 起步 | Vue Router

①手動配置(較少使用)

  1. 下載與導(dǎo)入vue-router
  2. 導(dǎo)入組件
  3. 創(chuàng)建routes路由規(guī)則(路徑和頁面一一對應(yīng))
  4. 創(chuàng)建路由對象
  5. 把路由對象掛載到App.vue
  6. 在頁面寫路由導(dǎo)航router-link (生成a標(biāo)簽)
  7. 在頁面寫路由出口router-view (生成占位盒子,用于顯示頁面內(nèi)容)

下面開始我們相關(guān)文件的創(chuàng)建

1.創(chuàng)建我們的腳手架(此時(shí)沒有選擇Router):

1d386e264033404e9d2e678b516ac38f.png

 2.準(zhǔn)備我們的App.vue文件:


  1. <template>
  2. <div>
  3. <!-- 頂部導(dǎo)航欄 -->
  4. <div class="footer_wrap">
  5. <a href="#/find">發(fā)現(xiàn)音樂</a>
  6. <a href="#/my">我的音樂</a>
  7. <a href="#/friend">朋友</a>
  8. </div>
  9. <!-- 下面內(nèi)容 -->
  10. <div class="top"></div>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. methods: {}
  16. }
  17. </script>
  18. <style scoped>
  19. body,
  20. html {
  21. margin: 0;
  22. padding: 0;
  23. }
  24. .footer_wrap {
  25. position: fixed;
  26. left: 0;
  27. top: 0;
  28. display: flex;
  29. width: 100%;
  30. text-align: center;
  31. background-color: #333;
  32. color: #ccc;
  33. }
  34. .footer_wrap a,
  35. span {
  36. cursor: pointer;
  37. flex: 1;
  38. text-decoration: none;
  39. padding: 20px 0;
  40. line-height: 20px;
  41. background-color: #333;
  42. color: #ccc;
  43. border: 1px solid black;
  44. }
  45. .footer_wrap a:hover,
  46. span:hover {
  47. background-color: #555;
  48. }
  49. .top {
  50. padding-top: 62px;
  51. }
  52. .footer_wrap .router-link-active {
  53. background-color: #000;
  54. }
  55. </style>

3.在src下面新建views文件夾并創(chuàng)建我們需要的.vue文件

88e582b6196e48c0a1c697bfc26d9869.png

3.1 find.vue


  1. <template>
  2. <div>
  3. <div class="nav_main">
  4. <router-link to="/Ranking">排行</router-link>
  5. <router-link to="/Recommend">推薦</router-link>
  6. <router-link to="/SongList">歌單</router-link>
  7. </div>
  8. <div style="1px solid red;">
  9. <router-view></router-view>
  10. </div>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'find',
  16. }
  17. </script>
  18. <style scoped>
  19. .nav_main {
  20. background-color: red;
  21. color: white;
  22. padding: 10px 0;
  23. }
  24. .nav_main a {
  25. text-align: center;
  26. text-decoration: none;
  27. color: white;
  28. font-size: 12px;
  29. margin: 7px 17px 0;
  30. padding: 0px 15px 2px 15px;
  31. height: 20px;
  32. display: inline-block;
  33. line-height: 20px;
  34. border-radius: 20px;
  35. }
  36. .nav_main a:hover {
  37. background-color: brown;
  38. }
  39. .nav_main .router-link-active{
  40. background-color: brown;
  41. }
  42. </style>

 3.2 my.vue


  1. <template>
  2. <div>
  3. <img src="../assets/my.png" alt="" width="100%">
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'my',
  9. };
  10. </script>
  11. <style scoped>
  12. </style>

3.3 friend.vue


  1. <template>
  2. <div>
  3. <ul>
  4. <li>這是當(dāng)前頁面 query 接收到的參數(shù):
  5. <span>姓名:{{ $route.query.name }}</span> --
  6. <span>年齡:{{$route.query.age}}</span>
  7. </li>
  8. <li>這是當(dāng)前頁面 params 接收到的參數(shù):
  9. <!-- <span>姓名:{{ $route.params.name }}</span> --
  10. <span>年齡:{{ $route.params.age }}</span> -->
  11. </li>
  12. </ul>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'friend',
  18. };
  19. </script>
  20. <style scoped>
  21. </style>

3.4 notfound.vue


  1. <template>
  2. <div class="box">
  3. <h1>這是一個(gè) 404 頁面</h1>
  4. <img src="../assets/404.png" alt="">
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'notfound',
  10. data() {
  11. return {
  12. };
  13. },
  14. };
  15. </script>
  16. <style scoped>
  17. .box {
  18. display: flex;
  19. flex-direction: column;
  20. justify-content: center;
  21. align-items: center;
  22. }
  23. </style>

 4.準(zhǔn)備圖片素材(所有素材可私信博主獲?。?

3febf3f9755241a3b8bafccedb0f646e.gif

 5.所有準(zhǔn)備工作做完現(xiàn)在開始我們的文件配置

 1.下載與導(dǎo)入vue-router

  • npm i vue-router@3.6.5

  • 導(dǎo)入vue-router (在main.js中)


  1. //main.js中導(dǎo)入
  2. // 0.導(dǎo)入路由
  3. import VueRouter from 'vue-router'
  4. // 使用vue的插件,都需要調(diào)用Vue.use()
  5. Vue.use(VueRouter)

2.導(dǎo)入組件

@符號代表 /src 文件夾的絕對路徑,在腳手架中文件比較多的時(shí)候,使用這個(gè)@符號會更加的方便

 在main.js中導(dǎo)入


  1. // 導(dǎo)入組件
  2. import find from '@/views/find.vue'
  3. import friend from '@/views/friend.vue'
  4. import my from '@/views/my.vue'
  5. import notfound from '@/views/notfound.vue'

3.創(chuàng)建路由規(guī)則

路由規(guī)則作用: 設(shè)置 url 和 組件 對應(yīng)的規(guī)則

 在main.js中寫入


  1. // 路由規(guī)則
  2. const routes = [
  3. { path: '/find', component: find },
  4. { path: '/friend', name: 'friend', component: friend },
  5. { path: '/my', component: my },
  6. { path: '/notfound', component: notfound },
  7. ]

4.創(chuàng)建路由對象

路由對象: 幫你管理這些路由規(guī)則

 在main.js中寫入


  1. // 創(chuàng)建路由對象
  2. const router = new VueRouter({
  3. routes// (縮寫) 相當(dāng)于 routes: routes
  4. })

 5.掛載路由到根組件

掛載到根組件作用:讓你的整個(gè)應(yīng)用都有路由功能

 在main.js中寫入


  1. // 掛載路由到根組件
  2. new Vue({
  3. router,
  4. render: h => h(App)
  5. }).$mount('#app')

6.在頁面寫路由導(dǎo)航router-link

作用與a標(biāo)簽一樣實(shí)現(xiàn)跳轉(zhuǎn),好處:當(dāng)點(diǎn)擊鏈接的時(shí)候自帶一個(gè)專屬類名 

在App.vue中我們將傳統(tǒng)的a標(biāo)簽進(jìn)行替換:

4970109a68ec4481b7b4ce6d015fb05e.png

替換a標(biāo)簽原因:便于我們做專屬效果 

 我們選中點(diǎn)擊的超鏈接做觸發(fā)效果:

457bf5ab19d14e1196aae8ebe7fbb272.png

 7在頁面寫路由出口router-view

占位盒子,用于渲染路由匹配到的組件

(<router-view> : 是vue內(nèi)置的一個(gè)組件,會自動替換成路由匹配的組件 )

0487e6a9e90f47259742157cc9bf927f.png

好了一個(gè)最最最基本的路由就被我們制作完成啦!下面我們來看看效果:

b90436a4b4b043e0972a686ec5d6c7b9.gif

上述的操作有些許麻煩,下面我們來使用我們開發(fā)中常用的自動配置方法

②自動配置(推薦使用)

創(chuàng)建腳手架方式與手動配置類似,唯一不同是此處必須選擇Router

8da9a6240ba34b7d8231d7e3d1329023.png

對比手動模式:

b4e1986802914f9682f82f40bd819859.png

 此刻:腳手架已經(jīng)幫我們創(chuàng)建好了Router路由不需要我們下載與導(dǎo)入vue-router了

只需要寫:

  1. 導(dǎo)入組件
  2. 配置路由規(guī)則
  3. 路由導(dǎo)航
  4. 路由出口

并且為了進(jìn)一步的封裝我們的配置信息,我們的配置代碼將寫在router/index.js下,不再是全部寫在main.js下。

1.導(dǎo)入組件(index.js中)


  1. import find from '@/views/find.vue'
  2. import friend from '@/views/friend.vue'
  3. import my from '@/views/my.vue'
  4. import notfound from '@/views/notfound.vue'

2.配置路由規(guī)則(index.js中)


  1. { path: '/find', component: find },
  2. { path: '/friend', name: 'friend', component: friend },
  3. { path: '/my', component: my },
  4. { path: '/notfound', component: notfound }

 3.路由導(dǎo)航(直接cv我們之前的App.vue文件)


  1.       <router-link to="/find">發(fā)現(xiàn)音樂</router-link>
  2.       <router-link to="/my">我的音樂</router-link>
  3.       <router-link to="/friend">朋友</router-link>

 4.路由出口(App.vue中


  1. <div class="top">
  2. <router-view></router-view>
  3. </div>

效果查看:

a0b3530b690c4a9ca6249c14b002d6b0.gif


總結(jié)手動自動配置區(qū)別:

自動配置省去了一些固定不變的操作,我們不需要寫繁瑣且固定的代碼,只需要寫不同的代碼。且代碼書寫的位置都給我們設(shè)置好了,我們直接遵守該規(guī)范書寫代碼即可


三、路由重定向與二級路由

1.路由重定向

路由重定向官方文檔:重定向和別名 | Vue Router

  • 重定向應(yīng)用場景: 頁面輸入根路徑/ , 自動跳轉(zhuǎn)到首頁

  • 注意點(diǎn) : 重定向只是修改路徑, 還需要單獨(dú)去設(shè)置路由匹配規(guī)則

重定向命令:


  1. {
  2. path: '/',
  3. /*
  4. (1)重定向只是修改頁面路徑。 輸入 / 會重定向到 /路徑
  5. (2)只有component才會讓vue去尋找路由匹配頁面。所以設(shè)置了重定向,還需要單獨(dú)設(shè)置匹配規(guī)則
  6. */
  7. redirect: "路徑"
  8. },

1. 就拿我們剛才創(chuàng)建的舉例:

bef58f0f03f0422bad9ff7ff552333ca.png

 實(shí)現(xiàn)效果:當(dāng)我在瀏覽器中打開的時(shí)候我沒有輸入任何路徑,vue自動幫我們跳轉(zhuǎn)到了 my.vue這個(gè)頁面組件

fc6d192e015f4d028f6d8e601a9ab2a1.png

 2.也可以利用重定向來設(shè)置當(dāng)我們路徑錯(cuò)誤提示404頁面:

ec7212bfaf874c898e43147ff21dcb43.png

實(shí)現(xiàn)效果:當(dāng)我任意輸入沒有匹配的路徑,自動幫我們跳轉(zhuǎn)到了notfound.vue這個(gè)組件

4544b2c521bc475998d8f3220f43f714.gif

2.二級路由

實(shí)現(xiàn)頁面中存在第二級的跳轉(zhuǎn)

寫法(拿我們上述的案例實(shí)操,需要素材可私信博主喔):

①在index.js中引入


  1. // 導(dǎo)入二級路由
  2. import Ranking from '@/views/second/Ranking.vue'
  3. import Recommend from '@/views/second/Recommend.vue'
  4. import SongList from '@/views/second/SongList.vue'

②在需要引用的組件中使用:


  1. //格式:
  2. {
  3. path: '路徑', component: 組件名, children: [
  4. //此處填寫二級路由的路徑信息
  5. ]
  6. }

  1. {
  2. path: '/find', component: find, children: [
  3. {path:'/',redirect:'/SongList'},
  4. { path: '/Ranking', component: Ranking },
  5. { path: '/Recommend', component: Recommend },
  6. { path: '/SongList', component: SongList }
  7. ]
  8. }

③寫路由導(dǎo)航與出口

bde6628a95f54fb2b73ef7ea54cb05f3.png

 查看效果:

fae7f2b60cab46599c9d7a4b142de98a.gif

 可以看到:當(dāng)我們點(diǎn)擊一級路由之后,我們還可以點(diǎn)擊二級路由到我們的專屬頁面中


 四、路由跳轉(zhuǎn)傳參

有兩種跳轉(zhuǎn)傳參方式:

  • 聲明式導(dǎo)航
  • 編程式導(dǎo)航

1.聲明式導(dǎo)航:

①query寫法:

在路徑中加參數(shù)信息即可

      <router-link to="/路徑?參數(shù)名=參數(shù)值&參數(shù)名=參數(shù)值</router-link>

接收信息:

在觸發(fā)的組件中書寫{{ $route.query.屬性名}}接收

 舉個(gè)例子:

026595d1cedd4c02ab86a4f99188edd8.png

②params寫法:

在index.jsx文件中寫:參數(shù)名。在需要傳遞的路由路徑中寫參數(shù)值

47aaff039df84bc990dd7f3f2ad9b74b.png

接收信息:

在觸發(fā)的組件中書寫{{ $route.params.屬性名}}接收

 6da470911546454488c229af441d7d78.png

 實(shí)現(xiàn)效果:

c2acf124d4cf44fb96dbc98e71d804c1.png


2.編程式導(dǎo)航

①query寫法:

結(jié)構(gòu):


  1. this.$router.push({
  2. path: '/路徑',
  3. query: { 屬性名: '屬性值'}
  4. })

接收信息:

在觸發(fā)的組件中書寫{{ $route.query.屬性名}}接收

舉個(gè)例子:

c55bce2fbbaf4910812fa99a15ef760b.png

 ②params寫法

結(jié)構(gòu):


  1. this.$router.push({
  2. name: '我們注冊路徑的組件名',//寫path獲取不到值?。?!
  3. query: { 屬性名: '屬性值'}
  4. })

 注意點(diǎn):寫path獲取不到值,需要用name

接收信息:

在觸發(fā)的組件中書寫{{ $route.params.屬性名}}接收

5fa2f0cd7c87446aadd377efaabe0784.png



藍(lán)藍(lán)設(shè)計(jì)建立了UI設(shè)計(jì)分享群,每天會分享國內(nèi)外的一些優(yōu)秀設(shè)計(jì),如果有興趣的話,可以進(jìn)入一起成長學(xué)習(xí),請加藍(lán)小助,微信號:ben_lanlan,報(bào)下信息,藍(lán)小助會請您入群。歡迎您加入噢~~希望得到建議咨詢、商務(wù)合作,也請與我們聯(lián)系01063334945。


分享此文一切功德,皆悉回向給文章原作者及眾讀者.
免責(zé)聲明:藍(lán)藍(lán)設(shè)計(jì)尊重原作者,文章的版權(quán)歸原作者。如涉及版權(quán)問題,請及時(shí)與我們?nèi)〉寐?lián)系,我們立即更正或刪除。


藍(lán)藍(lán)設(shè)計(jì)www.bouu.cn )是一家專注而深入的界面設(shè)計(jì)公司,為期望卓越的國內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 、平面設(shè)計(jì)服務(wù)、UI設(shè)計(jì)公司、界面設(shè)計(jì)公司、UI設(shè)計(jì)服務(wù)公司、數(shù)據(jù)可視化設(shè)計(jì)公司、UI交互設(shè)計(jì)公司、高端網(wǎng)站設(shè)計(jì)公司、UI咨詢、用戶體驗(yàn)公司、軟件界面設(shè)計(jì)公司

分享本文至:

日歷

鏈接

個(gè)人資料

存檔