vue中父组件传值给子组件,父组件值改变,子组件不能重新渲染

1在子组件中用watch()监听值的改变,不同的类型的要用不同的监听方法

props: {
  echartStyle: {
    type: Object,
    default() {
      return {}
    }},
    titleText: {
      type: String,
      default: ''
    },
    tooltipFormatter: {
      type: String,
      default: ''
    },
    opinion: {
      type: Array,
      default() {
        return []
      }
    },
    seriesName: {
      type: String,
      default: ''
    },
    opinionData: {
      type: Array,
      default() {
        return []
      }
    }
}

//watch进行监听

watch:{
  titleText:function(newValue,oldValue){
    this.getChange();
  },
  echartStyle:{
    handler(newValue,oldValue){
       this.getChange();
    },
    deep:true
  },
  tooltipFormatter:function(newValue,oldValue){
    this.getChange();
  },
  opinion:{
    handler(newValue,oldValue){
      this.getChange();
    },
    deep:true //深度监听
  },
  seriesName:function(newValue,oldValue){
    this.getChange();
  },
  opinionData:{
    handler(newValue,oldValue){
      this.getChange();
    },
    deep:true
  }

},
2 在父组件中用ref="str" 来声明组件,然后通过this.$refs.str.method()在值改变的地方来调用子组件中的方法
来 重新渲染(暂时使用有bug,不能够及时渲染,父组件值已经改变了,但是子组件值仍然没有改变,不能够及时渲染)
   这个方法感觉props'接收数据在调用方法之后,明明父组件的值已经改变了,但是父组件在调用子组件方法时,数据仍然没有  接收到,调用之后才接收到,这个方法暂且没用,应该是声明ref的时候声明的是当前组件的实例,然后调用时调用的也是值未改变时的属性。这个没什么用,可以用来调用子组件方法。

<pie-chart2   ref="pieChart"
  :echartStyle="echartStyle"  :titleText="titleText" :tooltipFormatter="tooltipFormatter"
  :opinion="opinion"  :seriesName="seriesName" :opinionData="opinionData"

>
</pie-chart2>
refresh:function(){
  if(!this.bindData.data){
    this.bindData.data = this.bindData.configures;
  }
  this.bindData.id = this.bindData.moduleId ||'pir';
  if(this.bindData.data){
    alert(this.bindData.data.name);
    this.changeContent(this.bindData.data);
    this.getChartData(this.bindData.data);
    this.$refs.pieChart.getChange();
  }

},



原文:https://blog.csdn.net/qq_39692513/article/details/80791458 
 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue,当组件传递给子组件动态改变时,可以通过以下两种方式重新渲染组件。 第一种方式是在组件使用ref来声明组件,并通过this.$refs来调用组件的方法来重新渲染。具体步骤如下: 1. 在组件使用ref属性声明组件,例如ref="com"。 2. 在改变的地方,通过this.$refs.com.method()来调用组件的方法,重新渲染组件。 3. 为了确保DOM更新后再重新渲染组件,需要在调用组件方法的地方使用Vue.nextTick()方法。例如Vue.nextTick(() => { this.$refs.com.method() })。\[1\] 第二种方式是在组件使用watch来监听组件传递过来的对象,并在监听回调函数重新。具体步骤如下: 1. 在组件添加watch来监听组件传递过来的对象。例如watch(props.value, (newVal, oldValue) => { //重新 })。 2. 在监听回调函数重新,更新组件的数据。\[2\] 需要注意的是,在第二种方式,为了确保DOM更新后再重新渲染组件,可以使用Vue.nextTick()方法。例如在监听回调函数使用Vue.nextTick(() => { //重新 })。\[3\] #### 引用[.reference_title] - *1* *3* [vue中父组件传值给子组件组件改变组件重新渲染](https://blog.csdn.net/hukay22/article/details/123913499)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [vue3组件传值给子组件,组件无法实时更新组件传递](https://blog.csdn.net/qq_44472790/article/details/121457306)[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^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值