Vue项目中强制刷新页面的方法

1.刷新整个页面

this.$router.go(0)
window.location.reload();

2.使用v-if标记
如果是刷新某个子组件,则可以通过v-if指令实现。我们知道,当v-if的值发生变化时,组件都会被重新渲染一遍。因此,利用v-if指令的特性,可以达到强制刷新组件的目的。

<template>
    <comp v-if="refresh"></comp>
    <button @click="refreshComp()">刷新comp组件</button>
</template>
<script>
import comp from '@/views/comp.vue'
export default {
    name: 'parentComp',
    data() {
        return {
            refresh: true
        }
    },
    methods: {
        refreshComp() {
            // 移除组件
            this.refresh = false
            // 在组件移除后,重新渲染组件
            // this.$nextTick可实现在DOM 状态更新后,执行传入的方法。
            this.$nextTick(() => {
                this.refresh = true
            })
        }
    }
}
</script>

3.使用内置的this.$forceUpdate方法

组件内置this.$forceUpdate方法,使用前需要在配置中启用。

注意:forceUpdate只会强制更新页面,不会更新现有的计算属性。

import Vue from 'vue'
Vue.forceUpdate()
<template>
  <div>
    <button @click="handleUpdateClick()">Refresh当前组件</button>
  </div>
</template>
export default {
  methods: {
    handleUpdateClick() {
      // built-in
      this.$forceUpdate()
    }
  }
}

4.使用key-changing优化组件
原理很简单,vue使用key标记组件身份,当key改变时就是释放原始组件,重新加载新的组件。

<template>
  <div>
    <span :key="componentKey"></span>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        componentKey: 0
      }
    },
    methods: {
     forceRerender() {
        this.componentKey += 1 // 或者 this.componentKey = new Date();
      }
    }
  }
</script>

5.应用实例——进入页面输入框自动聚焦(页面有缓存时,只有第一次进入页面生效)
一般情况下,使用指令

<template>
  <div>
    <input
		placeholder="大家都在搜"
	    type="text"
	    maxlength="500"
	    v-model="inputInfo.msg"
	    @blur="resizeView"
	    v-focus
	 >
  </div>
</template>
<script>
  export default {
    data() {
      return {
        inputInfo: { // 输入框对象
        	num: 0, // 字数
            msg: '' // 内容
        },
      }
    },
    watch: {
        [`options.msg`] () {
            let length = utils.fancyCount2(this.inputInfo.msg);
            this.$set(this.inputInfo, 'num', length);
        }
    },
    directives: {
        focus: {
            // 指令的定义
            inserted: function(el) {
                el.focus();
            }
        }
    },
    methods: {
      /**
         * input元素失去焦点时触发
         */
        resizeView () {
        	document.body.scrollIntoView(true);
        },
    }
  }
</script>

但是在有缓存的页面,一般就只有第一次会聚焦,后面进入都不会聚焦,办法就是用第四种强制刷新输入框来聚焦

<template>
  <div>
    <input
		placeholder="大家都在搜"
	    type="text"
	    maxlength="500"
	    v-model="inputInfo.msg"
	    @blur="resizeView"
	    v-focus
	    :key="inputInfo.focus"
	 >
	 <button @click="handleUpdateClick()">Refresh当前组件</button>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        inputInfo: { // 输入框对象
        	num: 0, // 字数
            msg: '', // 内容
            focus: '',
        },
      }
    },
    activated () {
        this.inputInfo.focus = new Date().getTime();
    },
    methods: {
    handleUpdateClick() {
      // built-in
      this.inputInfo.focus = new Date().getTime();
    }
  }
</script>

6.使用provide/inject强制刷新
也是使用v-if原理,只是子组件中根据父组件中的值进行强制刷新

7.watch监听路由,当路由发生变化时,页面没有刷新,可以监听他的路由,当路由发生变化时,刷新页面或者触发动作,使得整个页面刷新

  watch: {
    $route: {
      handler(newVal, oldVal) {
        if (newVal.query.code !== oldVal.query.code)
        或者
        
        newVal.params.id !== odl.params.id
        {
          //调用组价
          // this.$emit("");
          //执行函数
          //this.bindwx();
        }
      }
    }
  }

子路由触发父路由的方法,如果有多层路由,需要由子路由发出动作再一层一层的传上去,
如孙子路由 this.$emit(“updatekeyFn”);

<view-router @updatekeyFn="updatekeyFn()"/>
  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3,可以使用`location.reload()`或者`this.$router.go(0)`来强制刷新页面。这两种方法都会重新加载整个页面,类似于按下Ctrl+F5进行强制刷新,可能会出现瞬间的空白页面,体验不好。另外,也可以使用provide/inject组合来实现页面的刷新。具体的方法是,在App.vue声明一个reload方法,通过控制router-view的显示或隐藏来控制页面的重新加载。然后,在需要刷新的页面使用inject注入App.vue组件提供的reload依赖,并直接调用reload方法即可实现页面的刷新。总结起来,以上所述就是Vue3刷新当前页面的三种方法。 然而,需要注意的是,尽管以上方法可以达到刷新页面的目的,但是会重新加载整个页面,可能会出现瞬间的空白页面,体验不好。因此,建议在使用时权衡利弊,选择最适合项目需求和用户体验的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3.2 项目刷新当前页面的三种方法](https://blog.csdn.net/weixin_42365757/article/details/126121541)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue项目刷新当前页面的三种方法](https://blog.csdn.net/weixin_44635214/article/details/106471392)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值