this.$refs.formValidate.resetFields() Cannot read properties of undefined (reading 'indexOf')
时间: 2023-11-12 20:15:22 浏览: 156
Vue 解决通过this.$refs来获取DOM或者组件报错问题
The error message "Cannot read properties of undefined (reading 'indexOf')" usually occurs when you are trying to access a property or invoke a method on an undefined value. In this case, it seems like you are trying to reset fields using `this.$refs.formValidate.resetFields()`, but `formValidate` is undefined.
To fix this issue, make sure that you have defined `formValidate` and it is properly assigned to the form reference using `ref` attribute in your template. Here's an example of how you can define and use `formValidate`:
In your template:
```html
<template>
<el-form ref="formValidate" :model="formData" :rules="formRules">
<!-- form fields -->
</el-form>
</template>
```
In your component's script:
```javascript
<script>
export default {
data() {
return {
formData: {}, // initialize form data object
formRules: {}, // define form validation rules
}
},
methods: {
resetForm() {
// make sure that this.$refs.formValidate is defined
if (this.$refs.formValidate) {
this.$refs.formValidate.resetFields();
}
}
},
}
</script>
```
By following this pattern, you should be able to resolve the error and reset the form fields successfully.
阅读全文