报这个错主要是因为子组件还没加载完成就对子组件进行赋值,推荐使用第一个

		this.$nextTick( ()=> {
			//修改子组件的内容
        });
        //或
        setTimeout(() => {
       		//修改子组件的内容
		}, 50);

父组件传值给子组件,子组件不能直接修改,会报错

//子组件修改父组件的值
this.$emit('名字','值');

//子组件调用父组件的方法
this.$emit('方法', val)
//或
this.$parent.fatherMethod();
//或
<child :fatherMethod="fatherMethod"></child>

props: {
      fatherMethod: {
        type: Function,
        default: null
      }
    },


//父组件修改子组件的值
<tag ref="xxx" @b='b'></tag>
this.$refs.xxx.a = 1

//父组件调用子组件的方法
this.$refs.xxx.b()

watch中设置参数说明:有变化才能被监听
deep:是否深度监听
immediate:是否在页面初始化时就启用,true:是

//监听files变化
 watch: {
  files: {
    handler (newValue, oldValue) {
      console.log(newValue)
      this.fileList = newValue
    },
    deep: true // 默认值是 false,代表是否深度监听
  }
}

//监听对象的变化
data() {
 return {
  files: {
   name: 'demo'
  }   
  }
},
watch: {
  files: {
	handler(newValue, oldValue) {
	 console.log(newValue)
	},
  	deep: true
  }
}

//监听对象的具体属性
data() {
  return {
  files: {
   name: 'demo'
  } 
    }
},
computed: {
  filesName() {
    return this.files.name
  }
},
watch: {
  filesName(newValue, oldValue) {
    console.log(newValue)
  }
}

Logo

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

更多推荐