码迷,mamicode.com
首页 > 其他好文 > 详细

vue选择性刷新组件&如何实现优雅的刷新页面

时间:2020-03-23 11:13:23      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:style   next   this   router   def   com   ext   scope   操作   

在开发项目的过程中,有时修改后台的数据变化可能不会及时更新到页面上,此时就需要我们刷新页面更新数据,但是直接调用刷新window.location.reload()可能对操作的体验不是很好,所以就需要下面的方法。

列举个场景,比如修改主体content内容,我想要刷新主体部分的组件,但是不刷新title和aside组件,怎么实现呢?

实现方法就是在想要刷新的组件中封装一个方法,当需要刷新页面时直接调用这个方法就可以无痕迹刷新这个组件(页面)!

代码:

1、在app.vue中封装方法,此时调用可以刷新整个系统(整个页面)

<template>
	<div class="app-file">
		<router-view v-if="isRouterAlive"></router-view>
	</div>
</template>

<script>
export default {
	provide() {
		return {
			reloadAll: this.reloadAll
		}
	},
	data() {
		return {
			isRouterAlive: true
		}
	},
	methods: {
		reloadAll() {
			this.isRouterAlive = false
			this.$nextTick(() => {
				this.isRouterAlive = true
			})
		}
	}
}
</script>

<style lang="scss" scoped>
</style>

2、第二部,在想要实现刷新方法的组件引用及调用

技术图片

 

 3、这样在调用就可以实现我们想要的效果,同理,也可应用到其他组件,如开头所列举的场景:

技术图片

 

 这样就可以实现选择性的刷新头部,侧边导航栏或者是主体内容了!

 

vue选择性刷新组件&如何实现优雅的刷新页面

标签:style   next   this   router   def   com   ext   scope   操作   

原文地址:https://www.cnblogs.com/x-llin/p/12550784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!