ElementUI遇到TS的问题Property ‘resetFields’ does not exist on type ‘Vue’
问题描述:在一个弹框里有一个 form 表单,用于增加或编辑页面内容。当我点击取消按钮时本身 form 里的内容都清空才对。但是我是用了 this.$refs[‘editForm’].resetFields()完全不起作用,甚至还报错,因为我是第一次使用 typescript,所以也是摸石头过河,大致错误提示如下:
Property 'resetFields' does not exist on type 'Vue | Element | Vue[] | Element[]'. Property 'resetFields' does not exist on type 'Vue'.Vetur(2339)
网上的方法,resetFields() 并不是 Vue 的方法,而是你某个组件的方法。所以你需要把 this.$refs[formName] 显式标注为那个组件类型,比如:
resetForm (formName) { (<WhateverForm>this.$refs[editForm]).resetFields() // 或者不喜欢一堆括号的话: const formRef = <WhateverForm>this.$refs[editForm] formRef.resetFields() }
然而并不管用。
不知道大家发现没,在图片中提示要用 any 类型,所以我就把他改成
const ref: any = this.$refs[editForm] ref.resetFields()
事实证明,我的写法对的,完美解决报错问题。
结语
第一次使用 typescript,一路踩坑一路填坑,不过总算有所收获,如果大家也想入门学习一下可以看我之前写的有关typescript文章。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » ElementUI遇到TS的问题Property ‘resetFields’ does not exist on type ‘Vue’
码云笔记 » ElementUI遇到TS的问题Property ‘resetFields’ does not exist on type ‘Vue’