Vue3實現(xiàn)刷新頁面局部內容的示例代碼
想要實現(xiàn)頁面的局部刷新,我們只需要實現(xiàn)局部組件(dom)的重新渲染。在Vue中,想要實現(xiàn)這一效果最簡便的方式方法就是使用v-if
指令。
在Vue2中我們除了使用v-if
指令讓局部dom的重新渲染,也可以新建一個空白組件,需要刷新局部頁面時跳轉至這個空白組件頁面,然后在空白組件內的beforeRouteEnter
守衛(wèi)中又跳轉回原來的頁面。
如下圖所示,如何在Vue3.X中實現(xiàn)點擊刷新按鈕實現(xiàn)紅框范圍內的dom重新加載,并展示對應的加載狀態(tài)。
由于Vue3.X中script setup
語法中組件內守衛(wèi)只有onBeforeRouteUpdate
及onBeforeRouteUpdate
兩個API,因此我們來借助v-if
指令使局部dom重新渲染來實現(xiàn)這一需求。
第一步:定義狀態(tài)標識
在全局狀態(tài)中定義一個isRouterAlive
標識刷新狀態(tài),根據(jù)isRouterAlive
變化來重新渲染。isLoading
標識加載狀態(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é)點重新渲染
<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 值,實現(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')"> 關閉其他 </div> <div class="arrow-down-item" @click="handleCommand('closeLeft')"> 關閉左側 </div> <div class="arrow-down-item" @click="handleCommand('closeRight')"> 關閉右側 </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 // 設置為false,卸載dom setTimeout(() => { // 此處采用了定時器,并沒有采用網上比較常見的nextTick appStore.isRouterAlive = true // 設置為true,重新掛載dom appStore.isLoading = false // 隱藏數(shù)據(jù)加載狀態(tài) }, 500) } else if (command === 'closeOther') { tabsStore.closeOther() } else { tabsStore.closeLeftOrRight(command) } } // …… </script> <style lang="scss" scoped> …… CSS樣式 </style>
到此這篇關于Vue3實現(xiàn)刷新頁面局部內容的示例代碼的文章就介紹到這了,更多相關Vue3 刷新頁面局部內容內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue的無縫滾動組件vue-seamless-scroll實例
本篇文章主要給大家講解了vue的無縫滾動組件vue-seamless-scroll的用法,需要的朋友參考學習下吧。2017-12-12一款移動優(yōu)先的Solid.js路由solid router stack使用詳解
這篇文章主要為大家介紹了一款移動優(yōu)先的Solid.js路由solid router stack使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08解決vue的 v-for 循環(huán)中圖片加載路徑問題
今天小編就為大家分享一篇解決vue的 v-for 循環(huán)中圖片加載路徑問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09ElementUI?$notify通知方法中渲染自定義組件實現(xiàn)
這篇文章主要為大家介紹了ElementUI?$notify通知方法中渲染自定義組件實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06vue實現(xiàn)圖片路徑轉二進制文件流(binary)
這篇文章主要介紹了vue實現(xiàn)圖片路徑轉二進制文件流(binary),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06