Vue3動(dòng)態(tài)路由(響應(yīng)式帶參數(shù)的路由)變更頁面不刷新的問題解決辦法
背景
先說說問題,問題來源是因?yàn)槲业拈_源項(xiàng)目Maple-Boot項(xiàng)目的網(wǎng)站前端,因?yàn)轫?xiàng)目主打的內(nèi)容發(fā)布展示,所以其中的內(nèi)容列表頁會(huì)根據(jù)不同的菜單進(jìn)行渲染不同的路由。
這里路由path使用的是/blog/:menu?,通過menu的參數(shù)來渲染對(duì)應(yīng)的內(nèi)容,但是遇到了一個(gè)問題,在使用<RouterLink :to="{name: blog, params: {menu:java}}">跳轉(zhuǎn)時(shí),改變params的值,頁面不會(huì)重新渲染。
{ path: "/blog/:menu?", name: "blog", component: BlogView, },
官方答疑
查看官網(wǎng),得到結(jié)論如下:
官網(wǎng)地址:https://router.vuejs.org/zh/guide/essentials/dynamic-matching.html
使用帶有參數(shù)的路由時(shí)需要注意的是,當(dāng)用戶從 /users/johnny 導(dǎo)航到 /users/jolyne 時(shí),相同的組件實(shí)例將被重復(fù)使用。因?yàn)閮蓚€(gè)路由都渲染同個(gè)組件,比起銷毀再創(chuàng)建,復(fù)用則顯得更加高效。不過,這也意味著組件的生命周期鉤子不會(huì)被調(diào)用。
同時(shí)也給出了解決方案
方案一:使用watch
要對(duì)同一個(gè)組件中參數(shù)的變化做出響應(yīng)的話,你可以簡(jiǎn)單地 watch $route 對(duì)象上的任意屬性,在這個(gè)場(chǎng)景中,就是 $route.params
方案二:使用 beforeRouteUpdat
???????
或者,使用 beforeRouteUpdate 導(dǎo)航守衛(wèi),它還允許你取消導(dǎo)航
我的解決方案
我復(fù)用的頁面是BlogView
,原始完整內(nèi)容如下,主要看不同的內(nèi)容,防止直接貼部分代碼有同學(xué)找不到頭腦,這里貼全部的內(nèi)容吧,很多引用是找不到的
<script setup> import {onMounted, reactive, watch} from "vue"; import { useRoute } from 'vue-router'; import Meta from "@/examples/Meta.vue"; import DefaultNavbar from "@/examples/navbars/NavbarDefault.vue"; import Header from "@/examples/Header.vue"; import DefaultFooter from "@/examples/footers/FooterDefault.vue"; import BlogIndex from "./Sections/BlogIndex.vue"; import {getWebMenuByPath} from "../../api/common"; const route = useRoute(); const state = reactive({ webMenuInfo: {}, isGetData: false }); onMounted(() => { getWebMenuByPathClick(route.params.menu); }); const getWebMenuByPathClick = (menuPath) => { getWebMenuByPath(menuPath).then(res => { state.webMenuInfo = res; state.isGetData = true; }); } </script> <template> <Meta v-if="state.isGetData" :webMenuInfo="state.webMenuInfo"/> <div class="container position-sticky z-index-sticky top-0 opacity-8"> <div class="row"> <div class="col-12"> <DefaultNavbar :sticky="true"/> </div> </div> </div> <Header> <div class="page-header min-height-400" :style="{ backgroundImage: `url(${state.webMenuInfo.image})` }" loading="lazy" > <span class="mask bg-gradient-dark opacity-3"></span> </div> </Header> <BlogIndex :menuPath="state.webMenuInfo.path"/> <DefaultFooter /> </template>
修改后的內(nèi)容
<script setup> import {onMounted, reactive, watch} from "vue"; import { useRoute } from 'vue-router'; import Meta from "@/examples/Meta.vue"; import DefaultNavbar from "@/examples/navbars/NavbarDefault.vue"; import Header from "@/examples/Header.vue"; import DefaultFooter from "@/examples/footers/FooterDefault.vue"; import BlogIndex from "./Sections/BlogIndex.vue"; import {getWebMenuByPath} from "../../api/common"; const route = useRoute(); const state = reactive({ webMenuInfo: {}, isGetData: false }); onMounted(() => { getWebMenuByPathClick(route.params.menu); }); const getWebMenuByPathClick = (menuPath) => { getWebMenuByPath(menuPath).then(res => { state.webMenuInfo = res; state.isGetData = true; }); } watch(() => route.params.menu, (newId, oldId) => { getWebMenuByPathClick(route.params.menu); }) </script> <template> <Meta v-if="state.isGetData" :webMenuInfo="state.webMenuInfo"/> <div class="container position-sticky z-index-sticky top-0 opacity-8"> <div class="row"> <div class="col-12"> <DefaultNavbar :sticky="true"/> </div> </div> </div> <Header> <div class="page-header min-height-400" :style="{ backgroundImage: `url(${state.webMenuInfo.image})` }" loading="lazy" > <span class="mask bg-gradient-dark opacity-3"></span> </div> </Header> <BlogIndex :menuPath="state.webMenuInfo.path" :key="state.webMenuInfo.path"/> <DefaultFooter /> </template>
變更點(diǎn)一:變更的點(diǎn)主要是加了watch
監(jiān)聽route.params
變化時(shí),重新請(qǐng)求數(shù)據(jù)。
watch(() => route.params.menu, (newId, oldId) => { getWebMenuByPathClick(route.params.menu); })
變更點(diǎn)二:在<BlogIndex>
子組件上添加:key="state.webMenuInfo.path"
,通過不同的key標(biāo)注為不同組件
<BlogIndex :menuPath="state.webMenuInfo.path" :key="state.webMenuInfo.path"/>
看下效果
通過路由/blog/article
可以看到背景圖和分類的數(shù)據(jù)查詢出來了
當(dāng)路由切換到/blog/nterview-fenbushi
,可以看到背景圖發(fā)生了變化,同時(shí)因?yàn)闆]有配置對(duì)應(yīng)的分類欄目,數(shù)據(jù)渲染為空的。
到此這篇關(guān)于Vue3動(dòng)態(tài)路由(響應(yīng)式帶參數(shù)的路由)變更頁面不刷新的問題解決辦法的文章就介紹到這了,更多相關(guān)Vue3動(dòng)態(tài)路由變更頁面不刷新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue基于session和github-oauth2實(shí)現(xiàn)登錄注冊(cè)驗(yàn)證思路詳解
通過 sessionId 可以在 session 表中獲取用戶的信息,此外,還利用 session 表實(shí)現(xiàn)了GitHub 的 OAuth2 第三方登錄,本文講解前端通過簡(jiǎn)單的方式實(shí)現(xiàn)一個(gè)基本的登錄注冊(cè)驗(yàn)證功能,感興趣的朋友跟隨小編一起看看吧2024-08-08登錄頁面的實(shí)現(xiàn)及跳轉(zhuǎn)代碼實(shí)例(vue-router)
在Vue.js中可以使用vue-router來實(shí)現(xiàn)前端路由,通過路由來跳轉(zhuǎn)頁面,這篇文章主要給大家介紹了關(guān)于登錄頁面的實(shí)現(xiàn)及跳轉(zhuǎn)(vue-router)的相關(guān)資料,需要的朋友可以參考下2023-12-12Vue實(shí)現(xiàn)一個(gè)動(dòng)態(tài)添加行的表格步驟詳解
在Vue組件中定義表格的數(shù)據(jù)模型,通常使用一個(gè)數(shù)組來存儲(chǔ)表格的數(shù)據(jù),每一行數(shù)據(jù)可以是一個(gè)對(duì)象,對(duì)象的屬性對(duì)應(yīng)表格的列,這篇文章主要介紹了Vue實(shí)現(xiàn)一個(gè)動(dòng)態(tài)添加行的表格步驟詳解,需要的朋友可以參考下2024-05-05Vue使用.sync 實(shí)現(xiàn)父子組件的雙向綁定數(shù)據(jù)問題
這篇文章主要介紹了Vue使用.sync 實(shí)現(xiàn)父子組件的雙向綁定數(shù)據(jù),需要的朋友可以參考下2019-04-04