vue 强制刷新子组件
问题:1)因为父组件有缓存等因素,子组件创建后没有释放,传入参数无效2)父组件的tab切换, 导致子组件一直存在, 没有刷新解决方法:只要让子组件强制刷新就可以获取到props传递过来的值了, 使用v-if的特性(销毁和重建)可以强制刷新子组件<template><div><child v-if="sonRefresh"></child></d
·
问题:
1)因为父组件有缓存等因素,子组件创建后没有释放,传入参数无效
2)父组件的tab切换, 导致子组件一直存在, 没有刷新
解决方法:
方案一: 只要让子组件强制刷新就可以获取到props传递过来的值了, 使用v-if的特性(销毁和重建)可以强制刷新子组件
如果是刷新某个子组件,则可以通过v-if指令实现。我们知道,当v-if的值发生变化时,组件都会被重新渲染一遍。因此,利用v-if指令的特性,可以达到强制刷新组件的目的。
<template>
<div>
<child v-if="sonRefresh"></child>
</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
components: {
child
},
data(){
return {
sonRefresh: true
}
},
methods: {
fatherMethod() {
console.log('测试');
},
// 下面这段内容写在父组件获取值的地方
//this.sonRefresh= false;
//this.$nextTick(() => {
// this.sonRefresh= true;
//});
}
};
</script>
方案二: 使用this.$forceUpdate强制重新渲染
如果要在组件内部中进行强制刷新,则可以调用this.forceUpdate强制重新渲染组件,从而达到更新目的。
<template>
<button @click="reload()">刷新当前组件</button>
</template>
<script>
export default {
name: 'comp',
methods: {
reload() {
this.$forceUpdate();
}
}
}
</script>
更多推荐
已为社区贡献9条内容
所有评论(0)