欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3實現(xiàn)刷新頁面局部內容的示例代碼

 更新時間:2022年07月13日 15:43:02   作者:WEB前端李志杰  
本文主要介紹了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)只有onBeforeRouteUpdateonBeforeRouteUpdate 兩個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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論