异常情况处置方案
文章目录
vue
ref
1.vscode报错 ‘show‘ does not exist on type ‘Vue | Element | Vue[] | Element[]‘. Property ‘show‘ do
this.$refs.modal.show
解决方法:将类型进行转换
(this.$refs.modal.show as any).show()
补充:
- ref
ref 被用来给元素或子组件注册引用信息。引用信息将会注册在父组件的 $refs 对象上。如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素;如果用在子组件上,引用就指向组件实例:
<!-- `vm.$refs.p` will be the DOM node -->
<p ref="p">hello</p>
<!-- `vm.$refs.child` will be the child component instance -->
<child-component ref="child"></child-component>
当 v-for 用于元素或组件的时候,引用信息将是包含 DOM 节点或组件实例的数组。
关于 ref 注册时间的重要说明:因为 ref 本身是作为渲染结果被创建的,在初始渲染的时候你不能访问它们 - 它们还不存在!$refs 也不是响应式的,因此你不应该试图用它在模板中做数据绑定。
- $ref
一个对象,持有注册过 ref attribute 的所有 DOM 元素和组件实例。
antD
form表单相关
1.Error in v-on handler: “TypeError: Cannot read property ‘resetFields‘ of undefined“
解决方法:对话是一个虚拟dom,visible!==true之前,dom未挂载在根节点上,此时refs的
原因:对话框表单未挂载到根节点上,导致无法识别resetFields方法
解决方法:
- resetfileds方法无法使用,让对话框完全开启后再使用resetfileds方法
this.visible = true;
(this.$refs.ruleForm as any).reserFields(); //ts语法,js可去掉类型转换
- ($nextTick dom下一次更新之后)
resetForm(formName) {
this.$nextTick(()=>{
this.$refs[formName].resetFields();
})
},
- (如果是第一次就点击新增就没必要reset, 根据元素undefined判断)
if (this.$refs[formName] !== undefined) {
this.$refs[formName].resetFields();
}
注意事项:对DOM一系列的js操作最好都要放进Vue.nextTick()的回调函数中