Vue3實(shí)現(xiàn)刷新頁面局部內(nèi)容的示例代碼
想要實(shí)現(xiàn)頁面的局部刷新,我們只需要實(shí)現(xiàn)局部組件(dom)的重新渲染。在Vue中,想要實(shí)現(xiàn)這一效果最簡便的方式方法就是使用v-if
指令。
在Vue2中我們除了使用v-if
指令讓局部dom的重新渲染,也可以新建一個(gè)空白組件,需要刷新局部頁面時(shí)跳轉(zhuǎn)至這個(gè)空白組件頁面,然后在空白組件內(nèi)的beforeRouteEnter
守衛(wèi)中又跳轉(zhuǎn)回原來的頁面。
如下圖所示,如何在Vue3.X中實(shí)現(xiàn)點(diǎn)擊刷新按鈕實(shí)現(xiàn)紅框范圍內(nèi)的dom重新加載,并展示對(duì)應(yīng)的加載狀態(tài)。
由于Vue3.X中script setup
語法中組件內(nèi)守衛(wèi)只有onBeforeRouteUpdate
及onBeforeRouteUpdate
兩個(gè)API,因此我們來借助v-if
指令使局部dom重新渲染來實(shí)現(xiàn)這一需求。
第一步:定義狀態(tài)標(biāo)識(shí)
在全局狀態(tài)中定義一個(gè)isRouterAlive
標(biāo)識(shí)刷新狀態(tài),根據(jù)isRouterAlive
變化來重新渲染。isLoading
標(biāo)識(shí)加載狀態(tài)。
import { defineStore } from 'pinia' export const useAppStore = defineStore({ id: 'app', state: () => ({ isRouterAlive: true, isLoading: false } as { isRouterAlive: boolean; isLoading: boolean }) })
第二步、借用v-if 指令讓dom節(jié)點(diǎn)重新渲染
<template> <div class="common-layout"> <el-container> <SideMenuView :collapse="isCollapse"></SideMenuView> <el-container> <NavMenuView v-model:collapse="isCollapse"></NavMenuView> <TabsView></TabsView> <!--核心 start--> <el-main v-loading="appStore.isLoading" element-loading-text="頁面加載中……" element-loading-background="rgba(0, 0, 0, 0.8)" > <router-view v-if="appStore.isRouterAlive"> </router-view> </el-main> <!--核心 end--> <el-footer>Footer</el-footer> </el-container> </el-container> </div> </template> <script setup lang="ts"> import SideMenuView from './SideMenuView.vue' import NavMenuView from './NavMenuView.vue' import TabsView from './TabsView.vue' import { useAppStore } from '@/stores/app' const appStore = useAppStore() const isCollapse = ref(false) </script> <style lang="scss" scoped> …… CSS樣式 </style>
第三步、修改isRouterAlive 值,實(shí)現(xiàn)dom的重新渲染
<template> <div class="tabs-item cursor-pointer arrow-down" ref="buttonRef" @click="onClickOutside" > <el-icon><ArrowDownBold /></el-icon> </div> <el-popover ref="popoverRef" trigger="hover" virtual-triggering :virtual-ref="buttonRef" > <div class="arrow-down-item" @click="handleCommand('refresh')">刷新</div> <div class="arrow-down-item" @click="handleCommand('closeOther')"> 關(guān)閉其他 </div> <div class="arrow-down-item" @click="handleCommand('closeLeft')"> 關(guān)閉左側(cè) </div> <div class="arrow-down-item" @click="handleCommand('closeRight')"> 關(guān)閉右側(cè) </div> </el-popover> </template> <script setup lang="ts"> import { CloseBold, ArrowDownBold } from '@element-plus/icons-vue' import type { MenuItem } from '@/interface/menu' import { useMenuRouterStore } from '@/stores/menu-router' import { useTabsStore } from '@/stores/tabs' import { useAppStore } from '@/stores/app' const router = useRouter() const menuRouterStore = useMenuRouterStore() const tabsStore = useTabsStore() const appStore = useAppStore() // tabs功能操作 const buttonRef = ref() const popoverRef = ref() const onClickOutside = () => { unref(popoverRef).popperRef?.delayHide?.() } const handleCommand = (command: string) => { if (command === 'refresh') { appStore.isLoading = true // 展示數(shù)據(jù)加載狀態(tài) appStore.isRouterAlive = false // 設(shè)置為false,卸載dom setTimeout(() => { // 此處采用了定時(shí)器,并沒有采用網(wǎng)上比較常見的nextTick appStore.isRouterAlive = true // 設(shè)置為true,重新掛載dom appStore.isLoading = false // 隱藏?cái)?shù)據(jù)加載狀態(tài) }, 500) } else if (command === 'closeOther') { tabsStore.closeOther() } else { tabsStore.closeLeftOrRight(command) } } // …… </script> <style lang="scss" scoped> …… CSS樣式 </style>
到此這篇關(guān)于Vue3實(shí)現(xiàn)刷新頁面局部內(nèi)容的示例代碼的文章就介紹到這了,更多相關(guān)Vue3 刷新頁面局部內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue的無縫滾動(dòng)組件vue-seamless-scroll實(shí)例
本篇文章主要給大家講解了vue的無縫滾動(dòng)組件vue-seamless-scroll的用法,需要的朋友參考學(xué)習(xí)下吧。2017-12-12一款移動(dòng)優(yōu)先的Solid.js路由solid router stack使用詳解
這篇文章主要為大家介紹了一款移動(dòng)優(yōu)先的Solid.js路由solid router stack使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08解決vue的 v-for 循環(huán)中圖片加載路徑問題
今天小編就為大家分享一篇解決vue的 v-for 循環(huán)中圖片加載路徑問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue實(shí)現(xiàn)商品加減計(jì)算總價(jià)的實(shí)例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)商品加減計(jì)算總價(jià)的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08ElementUI?$notify通知方法中渲染自定義組件實(shí)現(xiàn)
這篇文章主要為大家介紹了ElementUI?$notify通知方法中渲染自定義組件實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue實(shí)現(xiàn)圖片路徑轉(zhuǎn)二進(jìn)制文件流(binary)
這篇文章主要介紹了vue實(shí)現(xiàn)圖片路徑轉(zhuǎn)二進(jìn)制文件流(binary),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06解決vue 項(xiàng)目引入字體圖標(biāo)報(bào)錯(cuò)、不顯示等問題
今天小編就為大家分享一篇解決vue 項(xiàng)目引入字體圖標(biāo)報(bào)錯(cuò)、不顯示等問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09基于Vue實(shí)現(xiàn)一個(gè)textarea幽靈建議功能
不知道你有沒有發(fā)現(xiàn)Bing AI聊天有個(gè)輸入提示功能,在用戶輸入部分內(nèi)容時(shí)后面會(huì)給出灰色提示文案,用戶只要按下tab鍵就可以快速添加提示的后續(xù)內(nèi)容,我將這個(gè)功能稱為幽靈建議,接下來我將用Vue框架來實(shí)現(xiàn)這個(gè)功能,需要的朋友可以參考下2023-09-09如何使用Webstorm和Chrome來調(diào)試Vue項(xiàng)目
這篇文章主要介紹了如何使用Webstorm和Chrome來調(diào)試Vue項(xiàng)目,對(duì)Vue感興趣的同學(xué),一定要看一下2021-05-05