學習源碼可以看我的個人前端學習筆記 (github.com):qdxzw/frontlearningNotes
覺得有幫助的同學,可以點心心支持一下哈(筆記是根據b站上學習的尚硅谷的前端視頻【張天禹老師】,記錄一下學習筆記,用于自己復盤,有需要學習的可以去b站學習原版視頻)
路由
一、對路由的理解
二、基本切換效果
- Vue3中要使用vue-router的最新版本,目前是4版本。
- 路由配置文件代碼如下:
import { createRouter, createWebHistory } from "vue-router";
import Home from "../pages/Home.vue";
import News from "../pages/News.vue";
import About from "../pages/About.vue";const router = createRouter({history: createWebHistory(),routes: [{path: "/home",component: Home,},{path: "/news",component: News,},{path: "/about",component: About,},],
});
export default router;
- main.ts代碼如下:
// 引入createApp用于創建應用(買個盆)
import { createApp } from "vue";
import router from "./router/index";
// 引入App根組件(買個根)
import App from "./App.vue";
const app = createApp(App);
app.use(router);
app.mount("#app");
- App.vue代碼如下
<template><div class="app"><h2 class="title">Vue路由測試</h2><!-- 導航區 --><div class="navigate"><RouterLink to="/home" active-class="active"><span>首頁</span></RouterLink><RouterLink to="/news" active-class="active"><span>新聞</span></RouterLink><RouterLink to="/about" active-class="active"><span>關于</span></RouterLink></div><!-- 展示區 --><div class="main-content"><RouterView></RouterView></div></div>
</template><script lang="ts" setup name="App">
import { RouterLink, RouterView } from 'vue-router'
</script><style scoped>
.title {text-align: center;
}
.navigate {width: 500px;text-align: center;margin: 0 auto;
}
.navigate span {display: inline-block;margin-right: 50px;width: 100px;height: 50px;line-height: 50px;background-color: blanchedalmond;text-decoration: none;/* color: ; */
}
.main-content {margin: 20px auto;text-align: center;width: 500px;height: 200px;border: 10px solid;background-color: aqua;
}
.active {color: salmon;
}
</style>
三、兩個注意點
- 路由組件通常存放在pages 或 views文件夾,一般組件通常存放在components文件夾。
- 通過點擊導航,視覺效果上“消失” 了的路由組件,默認是被卸載掉的,需要的時候再去掛載。
四、路由器工作模式
- history模式優點:URL更加美觀,不帶有#,更接近傳統的網站URL。缺點:后期項目上線,需要服務端配合處理路徑問題,否則刷新會有404錯誤。
const router = createRouter({history:createWebHistory(), //history模式/******/
})
- hash模式優點:兼容性更好,因為不需要服務器端處理路徑。缺點:URL帶有#不太美觀,且在SEO優化方面相對較差。
const router = createRouter({history:createWebHashHistory(), //hash模式/******/
})
五、to的兩種寫法
字符串、對象
<!-- 第一種:to的字符串寫法 -->
<router-link active-class="active" to="/home">主頁</router-link><!-- 第二種:to的對象寫法 -->
<router-link active-class="active" :to="{path:'/home'}">Home</router-link>
六、命名路由
作用:可以簡化路由跳轉及傳參(后面就講)。
給路由規則命名:
routes:[{name:'zhuye',path:'/home',component:Home},{name:'xinwen',path:'/news',component:News,},{name:'guanyu',path:'/about',component:About}
]
跳轉路由:
<!--簡化前:需要寫完整的路徑(to的字符串寫法) -->
<router-link to="/news/detail">跳轉</router-link><!--簡化后:直接通過名字跳轉(to的對象寫法配合name屬性) -->
<router-link :to="{name:'guanyu'}">跳轉</router-link>
七、嵌套路由
- 編寫News的子路由:Detail.vue
- 配置路由規則,使用children配置項:
const router = createRouter({history:createWebHistory(),routes:[{name:'zhuye',path:'/home',component:Home},{name:'xinwen',path:'/news',component:News,children:[{name:'xiang',path:'detail',component:Detail}]},{name:'guanyu',path:'/about',component:About}]
})
export default router
- 跳轉路由(記得要加完整路徑):
<router-link to="/news/detail">xxxx</router-link>
<!-- 或 -->
<router-link :to="{path:'/news/detail'}">xxxx</router-link>
- 記得去News組件中預留一個<router-view>
<template><div class="news"><!-- 導航區 --><ul><li v-for="news in newsList" :key="news.id"><RouterLink to="/news/detail">{{ news.title }}</RouterLink></li></ul><div class="news-detail"><RouterView /></div></div>
</template><script lang="ts" setup name="News">
import { reactive } from 'vue'
import { RouterLink, RouterView } from 'vue-router'
let newsList = reactive([{ id: 'dawd1', title: '1', content: '11' },{ id: 'dawd2', title: '2', content: '22' },{ id: 'dawd3', title: '3', content: '33' }
])
</script>
八、路由傳參
query參數
- 傳遞參數(query參數可以用path和name)
<!-- 跳轉并攜帶query參數(to的字符串寫法) -->
<router-link to="/news/detail?a=1&b=2&content=歡迎你">跳轉
</router-link><!-- 跳轉并攜帶query參數(to的對象寫法) -->
<RouterLink :to="{//name:'xiang', //用name也可以跳轉path:'/news/detail',query:{id:news.id,title:news.title,content:news.content}}"
>{{news.title}}
</RouterLink>
- 接收參數:
import {useRoute} from 'vue-router'
const route = useRoute()
// 打印query參數
console.log(route.query)
params參數
路由配置(如果傳遞的參數不是必須的,在后面加個?):
{name: "xinwen",path: "/news",component: News,children: [{name: "xiang",path: "detail/:id/:title/:content",component: Detail,},],},
- 傳遞參數(params參數只能用name)
<!-- 跳轉并攜帶params參數(to的字符串寫法) -->
<RouterLink :to="`/news/detail/001/新聞001/內容001`">{{news.title}}</RouterLink><!-- 跳轉并攜帶params參數(to的對象寫法) -->
<RouterLink :to="{name:'xiang', //用name跳轉params:{id:news.id,title:news.title,content:news.title}}"
>{{news.title}}
</RouterLink>
- 接收參數:
import {useRoute} from 'vue-router'
const route = useRoute()
// 打印params參數
console.log(route.params)
備注1:傳遞params參數時,若使用to的對象寫法,必須使用name配置項,不能用path。
備注2:傳遞params參數時,需要提前在規則中占位。
九、路由的props配置
作用:讓路由組件更方便的收到參數(可以將路由參數作為props傳給組件)
{name: "xiang",path: "detail/:id/:title/:content",component: Detail,// 第一種(傳遞值給路由組件):props的對象寫法,作用:把對象中的每一組key-value作為props傳給Detail組件// props:{a:1,b:2,c:3},// 第二種(只適用于props):props的布爾值寫法,作用:把收到了每一組params參數,作為props傳給Detail組件// props:true// 第三種(適用于props、query):props的函數寫法,作用:把返回的對象中每一組key-value作為props傳給Detail組件props(route) {return route.query;},},
使用defineProps進行接收
<template><ul><li>編號:{{ id }}</li><li>標題:{{ title }}</li><li>內容:{{ content }}</li></ul>
</template><script lang="ts" setup name="Detail">
defineProps(['id', 'title', 'content'])
</script>
十、 replace屬性
- 作用:控制路由跳轉時操作瀏覽器歷史記錄的模式。
- 瀏覽器的歷史記錄有兩種寫入方式:分別為push和replace:
-
- push是追加歷史記錄(默認值)。
- replace是替換當前記錄。
- 開啟replace模式:<RouterLink replace .......>News</RouterLink>
十一、編程式導航
路由組件的兩個重要的屬性:$route和$router變成了兩個hooks
<template><div class="news"><!-- 導航區 --><ul><li v-for="news in newsList" :key="news.id"><!-- <RouterLink to="/news/detail">{{ news.title }}</RouterLink> --><!-- 跳轉并攜帶params參數(to的字符串寫法) --><!-- <RouterLink :to="`/news/detail/001/新聞001/內容001`">{{news.title}}</RouterLink> --><!-- 跳轉并攜帶params參數(to的對象寫法) --><button @click="showNewsDetail(news)">點擊查看新聞</button><RouterLink:to="{name: 'xiang', //用name跳轉params: {id: news.id,title: news.title,content: news.title}}">{{ news.title }}</RouterLink></li></ul><div class="news-detail"><RouterView /></div></div>
</template><script lang="ts" setup name="News">
import { reactive } from 'vue'
import { RouterLink, RouterView, useRouter } from 'vue-router'
let newsList = reactive([{ id: 'dawd1', title: '1', content: '11' },{ id: 'dawd2', title: '2', content: '22' },{ id: 'dawd3', title: '3', content: '33' }
])
const router = useRouter()
interface NewsInter {id: stringtitle: stringcontent: string
}
function showNewsDetail(news: NewsInter) {router.replace({name: 'xiang', //用name跳轉params: {id: news.id,title: news.title,content: news.title}})
}
</script>
十二、重定向
- 作用:將特定的路徑,重新定向到已有路由。
- 具體編碼:
{path:'/',redirect:'/about'
}