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();
  }

},

3 在子组件上使用 v-if ="flag" (谢谢各位老哥的建议)

初始flag:true

修改data时

changData(data){

this.flag = false

this.data =data

this.$nextTick(()=>{

this.flag = true;

})

}

  

  

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐