两个常规解决方法和一个我最喜欢用的方法,解决v-if无法获取this.$refs。

一:

this.$nextTick(()=>{
  console.log(this.$refs)
})

二:

v-if改为v-show

三(我最喜欢的方法):

visible是父组件通过props传来的值,因为vue提示无法直接修改props,因此用watch监听visible,并用visible2替换。

  watch:{
    visible(i1,i2){
      this.visible2 = i1
      if(i1){
        this.init()
      }
    },
  },
  methods: {
    init(){
      this.$nextTick(()=>{
        if (this.type == 1){
          this.$refs.graphNode.init()
        }else if (this.type == 2){
          this.$refs.graphLink.init()
        }
      })
    }
  }

Logo

前往低代码交流专区

更多推荐