想要實作頁面的局部刷新,我們只需要實作局部元件(dom)的重新渲染。在Vue中,想要實現這效果最簡單的方式方法就是使用v-if
指令。
在Vue2中我們除了使用v-if
指令讓局部dom的重新渲染,也可以新建一個空白元件,需要刷新局部頁面時跳轉至這個空白元件頁面,然後在空白元件內的beforeRouteEnter
守衛中又跳轉回原來的頁面。
如何在Vue3.X中實作點擊刷新按鈕重新載入紅框內的DOM,並顯示對應的載入狀態? 。
由於Vue3.X中script setup
語法中元件內守衛只有onBeforeRouteUpdate
及onBeforeRouteUpdate
兩個API,因此我們來借助v-if
指令使局部dom重新渲染來實現這一需求。
在全域狀態中定義一個isRouterAlive
識別刷新狀態,根據isRouterAlive
變化來重新渲染。 isLoading
標識載入狀態。
import { defineStore } from 'pinia' export const useAppStore = defineStore({ id: 'app', state: () => ({ isRouterAlive: true, isLoading: false } as { isRouterAlive: boolean; isLoading: boolean }) })
<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>
<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 // 展示数据加载状态 appStore.isRouterAlive = false // 设置为false,卸载dom setTimeout(() => { // 此处采用了定时器,并没有采用网上比较常见的nextTick appStore.isRouterAlive = true // 设置为true,重新挂载dom appStore.isLoading = false // 隐藏数据加载状态 }, 500) } else if (command === 'closeOther') { tabsStore.closeOther() } else { tabsStore.closeLeftOrRight(command) } } // …… </script> <style lang="scss" scoped> …… CSS样式 </style>
以上是Vue3如何實作刷新頁面局部內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!