首頁  >  文章  >  web前端  >  Vue3如何實作刷新頁面局部內容

Vue3如何實作刷新頁面局部內容

王林
王林轉載
2023-05-26 17:31:263457瀏覽

想要實作頁面的局部刷新,我們只需要實作局部元件(dom)的重新渲染。在Vue中,想要實現這效果最簡單的方式方法就是使用v-if 指令。

在Vue2中我們除了使用v-if 指令讓局部dom的重新渲染,也可以新建一個空白元件,需要刷新局部頁面時跳轉至這個空白元件頁面,然後在空白元件內的beforeRouteEnter 守衛中又跳轉回原來的頁面。

如何在Vue3.X中實作點擊刷新按鈕重新載入紅框內的DOM,並顯示對應的載入狀態? 。

Vue3如何實作刷新頁面局部內容

Vue3如何實作刷新頁面局部內容

由於Vue3.X中script setup 語法中元件內守衛只有onBeforeRouteUpdateonBeforeRouteUpdate 兩個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 })
})

第二步、借用v-if 指令讓dom節點重新渲染

<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 &#39;./SideMenuView.vue&#39;
import NavMenuView from &#39;./NavMenuView.vue&#39;
import TabsView from &#39;./TabsView.vue&#39;
import { useAppStore } from &#39;@/stores/app&#39;
const appStore = useAppStore()
const isCollapse = ref(false)
</script>

<style lang="scss" scoped>
…… CSS样式
</style>

第三個步驟、修改isRouterAlive 值,實作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(&#39;refresh&#39;)">刷新</div>
    <div class="arrow-down-item" @click="handleCommand(&#39;closeOther&#39;)">
      关闭其他
    </div>
    <div class="arrow-down-item" @click="handleCommand(&#39;closeLeft&#39;)">
      关闭左侧
    </div>
    <div class="arrow-down-item" @click="handleCommand(&#39;closeRight&#39;)">
      关闭右侧
    </div>
  </el-popover>
</template>

<script setup lang="ts">
import { CloseBold, ArrowDownBold } from &#39;@element-plus/icons-vue&#39;
import type { MenuItem } from &#39;@/interface/menu&#39;
import { useMenuRouterStore } from &#39;@/stores/menu-router&#39;
import { useTabsStore } from &#39;@/stores/tabs&#39;
import { useAppStore } from &#39;@/stores/app&#39;
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 === &#39;refresh&#39;) {
    appStore.isLoading = true // 展示数据加载状态
    appStore.isRouterAlive = false // 设置为false,卸载dom
    setTimeout(() => { // 此处采用了定时器,并没有采用网上比较常见的nextTick
      appStore.isRouterAlive = true // 设置为true,重新挂载dom
      appStore.isLoading = false // 隐藏数据加载状态
    }, 500)
  } else if (command === &#39;closeOther&#39;) {
    tabsStore.closeOther()
  } else {
    tabsStore.closeLeftOrRight(command)
  }
}
// ……
</script>

<style lang="scss" scoped>
…… CSS样式
</style>

以上是Vue3如何實作刷新頁面局部內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除