场景:在父组件中表单提交数据,子组件中更新显示列表,子组件已经写完,再去添加prop略麻烦,尝试过location.reload()体验太差

模板部分

<recordDetail v-if="forceReload"></recordDetail>

data部分

forceReload:true

methods部分(表单验证成功后,先让组件消失,ajax返回成功后再让其显示)

this.$validator.validateAll().then((msg)=>{
  if(msg){
    this.forceReload=false;                    //先强制组件消失
    this.$ajax.get('/rest/admin/cases/create')
    .then((res)=>{
      this.forceReload=true;                   //强制组件再次渲染
    })
    .catch(function (error){});
  }else{
    this.$message({
      message: '填写不完整!',
      type: 'warning'
    });
  }
});

改进方法

this.forceReload = false
this.$nextTick(() => (this.forceReload = true))

新的方法:组件设置key,动态更新即可

<editor :key="key"></editor>
<el-button type="primary" @click="refresh">刷新组件</el-button>

this.key = Math.floor(Math.random()*10000000)
作者 铁血 汉子 2018年3月8日
2024/04/26/07:59:25am 2018/3/8/3:02:57
0 2557