Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Element UI自动完成组件表单校验冲突:如何解决v-model绑定失效问题?_创想鸟

Element UI自动完成组件表单校验冲突:如何解决v-model绑定失效问题?

element ui自动完成组件表单校验冲突:如何解决v-model绑定失效问题?

Element UI自动完成组件表单校验失效问题及解决方法

在使用Element UI的el-autocomplete组件构建表单时,经常遇到一个棘手问题:选择选项后,表单校验失败。即使输入框已显示正确值,表单仍提示校验未通过。本文将分析问题根源并提供有效解决方案。

问题描述:

el-autocomplete组件结合表单校验规则使用时,通过选择选项设置v-model值后,表单校验仍失败。

代码示例:

组件代码:

    

校验规则:

rules: {    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]},

相关函数:

selectoption(params) {  this.loginform.username = params.username;  this.loginform.password = atob(params.password);},onblur() {  this.loginform.username = this.selectuserinfo;},

问题根源分析:

直接赋值this.loginform.username = params.username和this.loginform.username = this.selectuserinfo无法触发Vue响应式系统更新,表单校验无法感知username属性变化。v-model绑定数据,但直接修改loginform对象属性绕过了Vue响应式机制。trigger: 'blur'也可能导致问题,因为el-autocomplete选择操作可能不会触发blur事件。

解决方法:

确保loginform.username始终保持响应式:

使用$set方法: 强制更新响应式数据。修改selectoption和onblur函数:

selectoption(params) {  this.$set(this.loginform, 'username', params.username);  this.loginform.password = atob(params.password);},onblur() {  this.$set(this.loginform, 'username', this.selectuserinfo);},

使用Object.assign()方法: 更新loginform对象:

selectoption(params) {  Object.assign(this.loginform, { username: params.username, password: atob(params.password) });},onblur() {  Object.assign(this.loginform, { username: this.selectuserinfo });},

更改校验触发事件: 将trigger修改为change或input:

rules: {    username: [{ required: true, message: '请输入用户名', trigger: 'change' }],    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]},

选择合适的方法取决于项目需求和代码风格。 通过以上方法,表单校验能够正确识别el-autocomplete组件选择后的数据变化,解决校验失败问题。

以上就是Element UI自动完成组件表单校验冲突:如何解决v-model绑定失效问题?的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1503113.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
jsPlumb连线ID错乱:页面滚动如何影响节点ID获取?
上一篇 2025年12月20日 01:32:48
Vite与Vue3项目中:如何动态获取public目录下指定文件夹的所有文件名?
下一篇 2025年12月20日 01:33:01

相关推荐

发表回复

登录后才能评论
关注微信