$forceUpdate的作用强制更新数据
在vue项目中,当你改变data数据源中的数据时,发现页面没有及时的去渲染,这时候可以调用this.$forceUpdate();方法去强制刷新页面
<template>
<div>{{user.name}}</div>
<button @click="changeName">设置名字</button>
</template>
<script>
export default {
data() {
return {
user: {}
}
},
methods: {
changeName() {
this.user.name = '小明';
// 调用之后触发 dom 改变
this.$forceUpdate();
}
}
}
</script>