解决 Vue 组件传值过程接收不成功的问题

第一个方式

  watch: {
    
    
    currentleCode: {
    
    
      handler() {
    
    
        this.setOptions()
      },
      deep: true
    }
  },

deep: true 使用深度监听监听传过来的数据 (如果数组的情况)

第二种方式

mounted() {
    
    
    this.getoptions()
  }
methods: {
    
    
    getoptions() {
    
    
      if (this.options) {
    
    
        this.setOptions()
      } else {
    
    
        setTimeout(() => {
    
    
          this.getoptions()
        }, 100)
      }
    }
   }

如果值传过来了 再进行后续部分 避免页面报错

额外注意的是 组件赋值的时候我们都喜欢用 =

又因为 = 不会触发Vue的set()和get()
所以推荐大家以后用set

// this.filterOptions[this.cuoduleCode] = res.data
this.$set(this.filterOptions, this.curreeCode, res.data)

Vue.set()和this.$ set()的区别在于:Vue.set()是将set函数绑定在Vue构造函数上,this.$set()是将set函数绑定在Vue原型上。

使用Vue.set()和this.$ set()可以解决不重新渲染的问题

猜你喜欢

转载自blog.csdn.net/weixin_50001396/article/details/123882553