使用Vue3和Pinia實現(xiàn)網(wǎng)頁刷新功能
概述
在現(xiàn)代 Web 開發(fā)中,保持用戶界面的動態(tài)性和響應性至關重要。當用戶觸發(fā)某些操作時,例如點擊按鈕或者完成表單提交,我們往往需要刷新頁面的一部分來展示最新的數(shù)據(jù)。本文將介紹如何使用 Vue 3 和 Pinia 來實現(xiàn)這一功能。
技術棧
- Vue 3
- Pinia (狀態(tài)管理)
目標
實現(xiàn)一個簡單的網(wǎng)頁刷新功能,當用戶點擊刷新按鈕時,頁面中的某個部分會重新加載以展示最新數(shù)據(jù)。
步驟
1. 創(chuàng)建項目
假設你已經(jīng)安裝了 Node.js 和 Vue CLI,可以使用 Vue CLI 創(chuàng)建一個新的 Vue 3 項目:
vue create my-refresh-app cd my-refresh-app npm install pinia @vue/router
2. 安裝依賴
安裝 Pinia 和 Vue Router,因為我們會使用 Pinia 來管理狀態(tài),并使用 Vue Router 來處理頁面的導航。
3. 配置 Pinia
創(chuàng)建一個 Pinia 的狀態(tài)管理倉庫來管理布局組件的配置,比如是否顯示刷新效果。
具體代碼如下:
import { defineStore } from 'pinia' const useLayoutSettingStore = defineStore('SettingStore', { state: () => { return { fold: false, // 是否折疊側邊欄 refresh: false // 刷新效果 } }, actions: { toggleRefresh() { this.refresh = !this.refresh; } } }) export default useLayoutSettingStore;
這里添加了一個 toggleRefresh
action 來切換 refresh
的狀態(tài)。
4. 設置 頂部刷新組件
現(xiàn)在我們需要在組件中使用這個倉庫,并且添加一個按鈕來觸發(fā)刷新。
具體代碼
<template> <el-button size="default" circle="false" @click="updateRefresh"> <el-icon> <Refresh></Refresh> </el-icon> </el-button> <el-button size="default" circle="false"> <el-icon> <FullScreen></FullScreen> </el-icon> </el-button> <el-button size="default" circle="false"> <el-icon> <Setting></Setting> </el-icon> </el-button> <img src="@/../public/favicon.ico" style="margin: 0 12px"> <el-dropdown :hide-on-click="false"> <span class="el-dropdown-link"> admin <el-icon class="el-icon--right"> <arrow-down /> </el-icon> </span> <template #dropdown> <el-dropdown-menu> <el-dropdown-item>退出登錄</el-dropdown-item> </el-dropdown-menu> </template> </el-dropdown> </template> <script setup lang="ts" name="setting"> // 獲取一下 配置 import useLayOutSettingStore from '@/store/modules/setting'; let layoutSettingStore = useLayOutSettingStore(); // 刷新按鈕點擊事件 const updateRefresh = () => { layoutSettingStore.refresh = !layoutSettingStore.refresh; } </script> <style scoped></style>
這里我們添加了一個 beforeEach
路由守衛(wèi)來檢查是否需要刷新當前路由。
5. 刷新的主頁面
這段代碼定義了一個 Vue 3 組件,該組件主要用于處理路由組件的過渡效果,并且具備一個刷新功能,用 v-if 來銷毀路由組件的入口。
<template> <!-- 路由組件出口位置 --> <router-view v-slot="{ Component }"> <transition name="fade"> <component :is="Component" v-if="flag" /> </transition> </router-view> </template> <script setup lang="ts" nam="main"> import { watch, ref, nextTick } from 'vue'; import useLayOutSettingStore from '@/store/modules/setting'; let layoutSettingStore = useLayOutSettingStore(); // 控制當前組件是否銷毀重建 let flag = ref(true); // 監(jiān)聽倉庫內部的數(shù)據(jù)是否發(fā)生變化,如果反生變化,說明用戶點擊過刷新按鈕 watch(() => layoutSettingStore.refresh, () => { // 點擊刷新按鈕: 路由組件需要銷毀 flag.value = false; nextTick(() => { flag.value = true; }) }) </script> <style scoped> .fade-enter-from { opacity: 0; transform: rotate(0deg) } .fade-enter-active { transition: all 1s; } .fade-enter-to { opacity: 1; transform: rotate(360deg); } </style>
導入依賴:
- 導入
watch
,ref
, 和nextTick
函數(shù),這些是 Vue 3 的 Composition API 中的核心函數(shù)。 - 導入
useLayOutSettingStore
,這是來自@/store/modules/setting
的 Pinia 存儲模塊。
- 導入
創(chuàng)建響應式引用:
let flag = ref(true);
創(chuàng)建一個響應式的布爾值flag
,初始值為true
。
監(jiān)聽數(shù)據(jù)變化:
- 使用
watch
函數(shù)監(jiān)聽layoutSettingStore.refresh
的變化。 - 當
refresh
發(fā)生變化時,先將flag
設置為false
,這會導致<component>
被隱藏,從而觸發(fā)組件的銷毀。 - 使用
nextTick
確保 DOM 更新后,再將flag
設置回true
,從而重新顯示組件。
- 使用
6. 測試
啟動應用并測試刷新功能:
npm run serve
打開瀏覽器,訪問 http://localhost:8080
,點擊“刷新”按鈕,查看頁面是否正確刷新。
結論
通過使用 Vue 3 和 Pinia,我們可以輕松地實現(xiàn)網(wǎng)頁的局部刷新功能。這種方法不僅可以提高用戶體驗,還可以使應用程序更加靈活和高效。
以上就是使用Vue3和Pinia實現(xiàn)網(wǎng)頁刷新功能的詳細內容,更多關于Vue3 Pinia網(wǎng)頁刷新的資料請關注腳本之家其它相關文章!
相關文章
vuejs如何清空表單數(shù)據(jù)、刪除對象中的空屬性公共方法
這篇文章主要介紹了vuejs如何清空表單數(shù)據(jù)、刪除對象中的空屬性公共方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03webpack vue項目開發(fā)環(huán)境局域網(wǎng)訪問方法
下面小編就為大家分享一篇webpack vue項目開發(fā)環(huán)境局域網(wǎng)訪問方法,具有很好的參考價值,希望對大家有所幫助,一起跟隨小編過來看看吧2018-03-03