vue3實(shí)現(xiàn)局部頁面刷新效果的示例詳解
需求描述
兩個(gè)VUE頁面,A為主頁面,B為子頁面?,F(xiàn)需求為,在A頁面點(diǎn)擊li數(shù)據(jù)后,B頁面內(nèi)容自動(dòng)改變。
一、分析
網(wǎng)上解決方案一般使用provide
和inject
來定義全局變量和方法,并在局部頁面中刷新。但多次嘗試后并不適合,以下是prop 父 子傳值以及全局方法配合實(shí)現(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)換為對(duì)象 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>
親測(cè)可用!
到此這篇關(guān)于vue3實(shí)現(xiàn)局部頁面刷新效果的示例詳解的文章就介紹到這了,更多相關(guān)vue3局部頁面刷新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目實(shí)現(xiàn)減少app.js和vender.js的體積操作
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)減少app.js和vender.js的體積操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11VUE項(xiàng)目中引入vue-router的詳細(xì)過程
vue-router 也叫 vue 路由,根據(jù)不同的路徑,來執(zhí)行不同的組件,這篇文章主要介紹了VUE項(xiàng)目中引入vue-router,需要的朋友可以參考下2023-05-05Vue3 style CSS 變量注入的實(shí)現(xiàn)
本文主要介紹了Vue3 style CSS 變量注入的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式
這篇文章主要給大家介紹了關(guān)于vue3中g(shù)etCurrentInstance不推薦使用及在<script?setup>中獲取全局內(nèi)容的三種方式,文中通過介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02vue3中vite的@路徑別名與path中resolve實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于vue3中vite的@路徑別名與path中resolve的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下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