uView
uView copied to clipboard
在uniapp-运行h5的环境下,$ref在组件内失效,导致uform的setrules方法调用失败
非page页面,而是自定义组件,放在mounted下的this.$refs.uForm.setRules(this.rules); 会报错 ”Cannot read property 'setRules' of undefined“,有什么好的解决办法吗
可以尝试一下nextTick
组件里用 watch:{ formType(newVal, oldVal){ if(newVal){ this.$nextTick(()=>{ this.resetForm(); this.$refs.uForm.setRules(this.form[newVal].rules); }); } } }
或者页面调用组件里使用 onReady() { let uFormRef = this.$refs.formRef.$refs.uForm; uFormRef.setRules(uFormRef.model.rules); },