vue3實現(xiàn)局部頁面刷新效果的示例詳解
需求描述
兩個VUE頁面,A為主頁面,B為子頁面。現(xiàn)需求為,在A頁面點擊li數(shù)據(jù)后,B頁面內(nèi)容自動改變。
一、分析
網(wǎng)上解決方案一般使用provide
和inject
來定義全局變量和方法,并在局部頁面中刷新。但多次嘗試后并不適合,以下是prop 父 子傳值以及全局方法配合實現(xiàn);
二、代碼示例
主頁面(A.vue)
<template> <div> <ul> <li v-for="item in list" :key="item.id" @click="handleClick(item)">{{ item.name }}</li> </ul> </div> </template> <script> export default { data() { return { list: [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' } ] }; }, methods: { handleClick(item) { // 轉(zhuǎn)換為對象 let obj = JSON.parse(JSON.stringify(item)); // this.$root.可以直接調(diào)用全局方法(App.vue 中的方法) this.$root.updateGlobalVariable(obj._source.id,obj._source.name); }, } }; </script>
子頁面(B.vue)
<template> <div> <p>{{ globalid }}</p> <p>{{ globalname }}</p> </div> </template> <script> export default { props:['globalid','globalname'], }; </script>
App頁面(App.vue)
<template> <div id="app"> <A :globalid="globalid" :globalid="globalid"></A> <B :globalname="globalname" :globalname="globalname"></B> </div> </template> <script> import a from './components/A.vue'; import b from './components/B.vue'; export default { name: 'App', components: { a, b }, data() { return { globalid: 'globalid Str ...', // 初始化全局變量 globalname: 'globalname Str ...' // 初始化全局變量 } }, methods:{ updateGlobalVariable(id,name) { this.globalid= id; // 更新全局變量的值 this.globalname= name; // 更新全局變量的值 // 重新加載頁面 this.$forceUpdate(); } } }; </script>
親測可用!
到此這篇關(guān)于vue3實現(xiàn)局部頁面刷新效果的示例詳解的文章就介紹到這了,更多相關(guān)vue3局部頁面刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項目實現(xiàn)減少app.js和vender.js的體積操作
這篇文章主要介紹了vue項目實現(xiàn)減少app.js和vender.js的體積操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式
這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式,文中通過介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02vue3中vite的@路徑別名與path中resolve實例詳解
這篇文章主要給大家介紹了關(guān)于vue3中vite的@路徑別名與path中resolve的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-02-02Vue3中SetUp函數(shù)的參數(shù)props、context詳解
我們知道setup函數(shù)是組合API的核心入口函數(shù),下面這篇文章主要給大家介紹了關(guān)于Vue3中SetUp函數(shù)的參數(shù)props、context的相關(guān)資料,需要的朋友可以參考下2021-07-07