Vue實(shí)現(xiàn)刷新當(dāng)前頁(yè)面的三種方式總結(jié)
背景
項(xiàng)目當(dāng)中如果做新增/修改/刪除等等操作通常情況下都需要刷新數(shù)據(jù)或者刷新當(dāng)前頁(yè)面.
思路
(1)如果頁(yè)面簡(jiǎn)單,調(diào)用接口刷新數(shù)據(jù)即可.
(2)如果頁(yè)面復(fù)雜,需要調(diào)用多個(gè)接口或者通知多個(gè)子組件做刷新,可以采用刷新當(dāng)前頁(yè)面的方式 下面整理了3種方式來(lái)實(shí)現(xiàn)刷新當(dāng)前頁(yè)面,每種方式的思路不同,各有優(yōu)缺點(diǎn)
實(shí)現(xiàn)
方式1-通過(guò)location.reload和$router.go(0)方法
(a)概述
通過(guò)location.reload和$router.go(0)都可以實(shí)現(xiàn)頁(yè)面刷新,它利用瀏覽器刷新功能,相當(dāng)于按下了f5鍵刷新頁(yè)面
優(yōu)點(diǎn):足夠簡(jiǎn)單
確定:會(huì)出現(xiàn)頁(yè)面空白,用戶體驗(yàn)不好
(b)代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"></script>
<script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue-router/3.5.3/vue-router.min.js" type="application/javascript"></script>
<style>
* {padding:0;margin:0;}
.container { padding: 10px;display: flex;flex-basis: auto;height: 100vh;box-sizing: border-box;}
.aside{ width:200px;background-color: #d3dce6; }
.main { flex: 1; }
</style>
</head>
<body>
<div id="app">
<router-view></router-view>
</div>
</body>
<script>
//框架頁(yè)
let Layout = {
created() {
console.log('框架頁(yè)加載')
},
template: `
<div class="container">
<div class="aside">左側(cè)菜單</div>
<div class="main"><router-view></router-view></div>
</div>
`
}
//首頁(yè)
let Home = {
template: `
<div>
首頁(yè)
<button @click="onClick">刷新</button>
</div>
`,
created() {
console.log('首頁(yè)加載')
},
methods: {
onClick(){
// 通localtion.reload或者this.$router.go(0)實(shí)現(xiàn)整體刷新頁(yè)面,會(huì)出現(xiàn)頁(yè)面閃爍
// location.reload()
this.$router.go(0)
}
},
}
//路由配置
let router = new VueRouter({
routes: [
{path: '/', component: Layout, children:[
{path: '', component: Home}
]}
]
})
Vue.use(VueRouter)
//根組件
new Vue({
router,
el: '#app'
})
</script>
</html>(c)預(yù)覽
方式2-通過(guò)空白頁(yè)面
(a)概述
通過(guò)$router.replace方法,跳轉(zhuǎn)一個(gè)空白頁(yè)面,然后再調(diào)回之前頁(yè)面,它利用vue-router切換頁(yè)面會(huì)把頁(yè)面銷毀并新建新頁(yè)面的特性
優(yōu)點(diǎn):不會(huì)出現(xiàn)頁(yè)面空白,用戶體驗(yàn)好
缺點(diǎn):地址欄會(huì)出現(xiàn)快速切換的過(guò)程
(b)代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"></script>
<script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue-router/3.5.3/vue-router.min.js" type="application/javascript"></script>
<style>
* {padding:0;margin:0;}
.container { padding: 10px;display: flex;flex-basis: auto;height: 100vh;box-sizing: border-box;}
.aside{ width:200px;background-color: #d3dce6; }
.main { flex: 1; }
</style>
</head>
<body>
<div id="app">
<router-view></router-view>
</div>
</body>
<script>
//框架頁(yè)
let Layout = {
created() {
console.log('框架頁(yè)加載')
},
template: `
<div class="container">
<div class="aside">左側(cè)菜單</div>
<div class="main"><router-view></router-view></div>
</div>
`
}
//首頁(yè)
let Home = {
template: `
<div>
首頁(yè)
<button @click="onClick">刷新</button>
</div>
`,
created() {
console.log('首頁(yè)加載')
},
methods: {
onClick(){
//使用replace跳轉(zhuǎn)后不會(huì)留下 history 記錄,并通過(guò)redirect傳遞當(dāng)前頁(yè)面的路徑
this.$router.replace(`/blank?redirect=${this.$route.fullPath}`)
}
},
}
//空白頁(yè)面
let Blank = {
created(){
console.log('空白頁(yè)加載')
//重新跳回之前的頁(yè)面
this.$router.replace(this.$route.query.redirect)
},
template: `
<div></div>
`
}
//路由配置
let router = new VueRouter({
routes: [
{path: '/', component: Layout, children:[
{path: '', component: Home}
]},
//配置空白頁(yè)面的路由
{path: '/blank', component: Layout, children:[
{path: '', component: Blank}
]}
]
})
Vue.use(VueRouter)
//根組件
new Vue({
router,
el: '#app'
})
</script>
</html>(c)預(yù)覽
方式3-通過(guò)provide和inject
(a)概述
通過(guò)在父頁(yè)面的<router-view></router-view>上添加v-if的控制來(lái)銷毀和重新創(chuàng)建頁(yè)面的方式刷新頁(yè)面,并且用到provide和inject實(shí)現(xiàn)多層級(jí)組件通信方式,父頁(yè)面通過(guò)provide提供reload方法,子頁(yè)面通過(guò)inject獲取reload方法,調(diào)用方法做刷新
優(yōu)點(diǎn):不會(huì)出現(xiàn)頁(yè)面空白,地址欄會(huì)不會(huì)出現(xiàn)快速切換的過(guò)程,用戶體驗(yàn)好
缺點(diǎn):實(shí)現(xiàn)稍復(fù)雜,涉及到provide和inject多層級(jí)組件間的通信,和v-if控制組件創(chuàng)建和銷毀,和$nextTick事件循環(huán)的應(yīng)用
(b)代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"></script>
<script src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue-router/3.5.3/vue-router.min.js" type="application/javascript"></script>
<style>
* {padding:0;margin:0;}
.container { padding: 10px;display: flex;flex-basis: auto;height: 100vh;box-sizing: border-box;}
.aside{ width:200px;background-color: #d3dce6; }
.main { flex: 1; }
</style>
</head>
<body>
<div id="app">
<router-view></router-view>
</div>
</body>
<script>
//框架頁(yè)
let Layout = {
template: `
<div class="container">
<div class="aside">左側(cè)菜單</div>
<!-- 通過(guò)v-if實(shí)現(xiàn)銷毀和重新創(chuàng)建組件 -->
<div class="main"><router-view v-if="isRouterAlive"></router-view></div>
</div>
`,
created() {
console.log('框架頁(yè)加載')
},
// 通過(guò)provide提供reload方法給后代組件
provide(){
return {
reload: this.reload
}
},
data(){
return {
isRouterAlive: true
}
},
methods: {
async reload(){
this.isRouterAlive = false
//通過(guò)this.$nextTick()產(chǎn)生一個(gè)微任務(wù),在一次dom事件循環(huán)后,重新創(chuàng)建組件
await this.$nextTick()
this.isRouterAlive = true
}
}
}
//首頁(yè)
let Home = {
template: `
<div>
首頁(yè)
<button @click="onClick">刷新</button>
</div>
`,
created() {
console.log('首頁(yè)加載')
},
//通過(guò)inject獲取祖先元素的reload方法
inject: ['reload'],
methods: {
onClick(){
this.reload()
}
},
}
//路由配置
let router = new VueRouter({
routes: [
{path: '/', component: Layout, children:[
{path: '', component: Home}
]}
]
})
Vue.use(VueRouter)
//根組件
new Vue({
router,
el: '#app'
})
</script>
</html>(c)預(yù)覽
以上就是Vue實(shí)現(xiàn)刷新當(dāng)前頁(yè)面的三種方式總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Vue刷新當(dāng)前頁(yè)面的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue + Element-ui的下拉框el-select獲取額外參數(shù)詳解
這篇文章主要介紹了Vue + Element-ui的下拉框el-select獲取額外參數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
用vue2.0實(shí)現(xiàn)點(diǎn)擊選中active其他選項(xiàng)互斥的效果
這篇文章主要介紹了用vue2.0實(shí)現(xiàn)點(diǎn)擊選中active其他選項(xiàng)互斥的效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
vue實(shí)現(xiàn)動(dòng)態(tài)按鈕功能
這篇文章主要介紹了vue實(shí)現(xiàn)動(dòng)態(tài)按鈕功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Vue單頁(yè)面應(yīng)用中實(shí)現(xiàn)Markdown渲染
這篇文章主要介紹了Vue單頁(yè)面應(yīng)用中如何實(shí)現(xiàn)Markdown渲染,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-02-02
vue子組件中使用window.onresize()只執(zhí)行一次問(wèn)題
這篇文章主要介紹了vue子組件中使用window.onresize()只執(zhí)行一次問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
element-ui中Table表格省市區(qū)合并單元格的方法實(shí)現(xiàn)
這篇文章主要介紹了element-ui中Table表格省市區(qū)合并單元格的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
基于vue實(shí)現(xiàn)網(wǎng)站前臺(tái)的權(quán)限管理(前后端分離實(shí)踐)
這篇文章主要介紹了基于vue實(shí)現(xiàn)網(wǎng)站前臺(tái)的權(quán)限管理(前后端分離實(shí)踐),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
vue路由攔截及頁(yè)面跳轉(zhuǎn)的設(shè)置方法
這篇文章主要介紹了vue路由攔截及頁(yè)面跳轉(zhuǎn)的設(shè)置方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05

