vue組件強制刷新的4種方案
前言:
Vue的雙向綁定屬于自動檔;在特定的情況下,需要手動觸發(fā)“刷新”操作,目前有四種方案可以選擇:
- 刷新整個頁面(最low的,可以借助route機制)
- 使用v-if標記(比較low的)
- 使用內置的forceUpdate方法(較好的)
- 使用key-changing優(yōu)化組件(最好的)
刷新整個頁面
this.$router.go(0); window.location.reload();
使用v-if標記
如果是刷新某個子組件,則可以通過v-if指令實現。我們知道,當v-if的值發(fā)生變化時,組件都會被重新渲染一遍。因此,利用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 狀態(tài)更新后,執(zhí)行傳入的方法。 this.$nextTick(() => { this.refresh = true }) } } } </script>
流程分析:
1、初始化的時候refresh值為 true,組件渲染;
2、當我們調用refreshComp時,refreshComp會立刻變?yōu)閒alse;
3、這個時候因為值為false組件就會停止渲染;
4、然后在nextTick中將refresh的值重新設置回去,組件重新渲染。
上面的流程主要有兩個重要的點需要理解:
1、必須要在nextTick以后才能更改,否則會看不到效果
在Vue中,DOM的更新周期即為一個tick,在同一個tick內Vue會搜集變化,然后在tick的最后會根據變化的值去更新節(jié)點,如果我們不等到next
tick,直接更新變量的值,不會觸發(fā)節(jié)點的更新。
2、當我們重新渲染的時候,Vue將會創(chuàng)建一個新的組件。Vue銷毀之前的重新創(chuàng)建新的意味著新的組件會重新走一遍生命周期。
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() } } }
forceUpdate只會強制更新頁面,不會更新現有的計算屬性。
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>
進入頁面輸入框自動聚焦
一般情況下,加上以下代碼就可以聚焦
<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元素失去焦點時觸發(fā) */ 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>
總結
到此這篇關于vue組件強制刷新的4種方案的文章就介紹到這了,更多相關vue組件強制刷新內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在vue中使用vant TreeSelect分類選擇組件操作
這篇文章主要介紹了在vue中使用vant TreeSelect分類選擇組件操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11在vue中nextTick用法及nextTick 的原理是什么
這篇文章主要介紹了在vue中nextTick用法及nextTick 的原理是什么,Vue.js 是一個流行的前端框架,它提供了一種響應式的數據綁定機制,使得頁面的數據與頁面的 UI 組件之間能夠自動同步,需要的朋友可以參考下2023-04-04