Vue 2 迁移到 Vue 3 时的 v-model 语法不兼容 问题,警告 Extraneous non-props attributes (modelValue)
[Vue warn]: Extraneous non-props attributes (modelValue) were passed to component but could not be automatically inherited because component renders fragment or text root nodes.这个警告的原因是Vue 3 中v-model的语法变化与组件存在多根节点Fragment共同导致的。错误原因分析v-model默认属性名改变在 Vue 3 中组件上的v-model默认绑定的 prop 是modelValue触发的事件是update:modelValue。而你的u-dialog组件中定义的 prop 是value且触发的是input事件这是 Vue 2 的语法。产生非 prop 特性因为父组件使用了v-model实际上向子组件传递了modelValue。但子组件的props中并没有modelValue所以 Vue 把它当成了“非 prop 的特性 (non-props attribute)”。多根节点无法继承你的template中有page-meta和view两个根节点构成了 Fragment。Vue 无法将未声明的非 prop 特性自动挂载到多个根节点上因此抛出了这个警告。方案一 将组件改造为符合 Vue 3 标准的v-model推荐将组件的value改为modelValue并将触发的事件从input改为update:modelValue。1. 修改u-dialog组件的templatetemplate !-- 将 value 改为 modelValue -- page-meta :page-stylemodelValue ? overflow:hidden;height:100vh;position:fixed; : / view v-ifmodelValue !-- 下面的代码保持不变 -- view classdialog-model clickhandleMaskClose touchmove.stop.prevent(){} wheel.stop.prevent(){} !-- ... --修改u-dialog组件的scriptexport default { name: u-dailog, // 建议加上 inheritAttrs: false防止以后传入 class/style 时因多根节点再次报错 inheritAttrs: false, props: { // 将 value 改为 modelValue modelValue: { type: Boolean, default: false }, // ... 其他 props 保持不变 }, // 声明 emits (Vue 3 推荐做法) emits: [update:modelValue, cancel, confirm], data() { return {} }, // ... computed 保持不变 methods: { onCancel() { // 将 input 改为 update:modelValue this.$emit(update:modelValue, false) this.$emit(cancel) }, onConfirm() { // 将 input 改为 update:modelValue this.$emit(update:modelValue, false) this.$emit(confirm) }, handleMaskClose(){ this.maskClose this.onCancel() } } }修改后父组件的u-dialog v-modelshowDialog/u-dialog无需任何改动即可完美运行。方案二保留组件的value属性修改父组件的用法如果你不想修改u-dialog内部的 prop 名称可以利用 Vue 3 支持的v-model自定义参数特性。1. 修改父组件的调用方式给v-model加上参数:value明确告诉 Vue 绑定value属性。!-- 将 v-model 改为 v-model:value -- u-dialog v-model:valueshowDialog/u-dialog2. 修改u-dialog组件的script触发事件虽然属性名保持了value但 Vue 3 的事件触发机制变了需要将input改为update:value。methods: { onCancel() { // 将 input 改为 update:value this.$emit(update:value, false) this.$emit(cancel) }, onConfirm() { // 将 input 改为 update:value this.$emit(update:value, false) this.$emit(confirm) }, handleMaskClose(){ this.maskClose this.onCancel() } }你遇到的问题是典型的Vue 2 迁移到 Vue 3 时的v-model语法不兼容问题。建议直接采用方案一将组件彻底升级为 Vue 3 的标准写法同时加上inheritAttrs: false来规避多根节点Fragment带来的潜在特性继承警告。以 input 实现完整实例// components/u-input/u-input.vue template view classinput-wrapper !-- 用 key 强制重新渲染解决 uni-app input 值不同步的问题 -- input classuni-input :keyinputKey :placeholderplaceholder :valuemodelValue :typeinputType :passwordshowPassword inputhandleInput :maxlengthmaxlength v-bind$attrs / uni-icons v-ifshowClearIcon typeclose classuni-icon :sizeiconSize :coloriconColor clickhandleClearValue / uni-icons v-ifisPassWord classuni-icon :sizeiconSize :coloriconColor clickchangePassword :typeshowPassword ? eye-filled : eye-slash-filled / /view /template script export default { name: u-input, inheritAttrs: false, emits: [update:modelValue], props: { modelValue: { type: [String, Number], default: , }, type: { type: String, default: text, }, placeholder: { type: String, default: 请输入, }, iconSize: { type: Number, default: 20 }, //显式声明避免被 $attrs 透传出问题 maxlength: { type: [String, Number], default: -1, }, iconColor: { type: String, default: #808080 } }, data() { return { showPassword: false, showClearIcon: false, inputKey: 0, // 用于强制刷新 input }; }, computed: { isPassWord() { return this.type password || this.type safe-password; }, inputType() { return this.isPassWord ? text : this.type; } }, watch: { // 监听 value 变化保持清除按钮状态同步 modelValue: { immediate: true, handler(val) { this.showClearIcon val val.length 0; }, }, //type 变化时重置密码可见状态 isPassWord: { immediate: true, handler(val) { this.showPassword val; }, } }, methods: { handleInput(event) { const val event.detail.value; this.$emit(update:modelValue, val); this.showClearIcon val.length 0; }, handleClearValue() { this.$emit(update:modelValue, ); this.showClearIcon false; this.inputKey; }, changePassword() { this.showPassword !this.showPassword; }, }, }; /script style scoped langscss .input-wrapper { display: flex; flex-direction: row; align-items: center; width: 100%; } .uni-input { flex: 1; width: 100%; } .uni-icon { margin: 0 $uni-spacing-row-sm; } /style使用template u-input v-modeluser_phone classform-item-input typetel placeholder请输入手机号 maxlength11 /u-input /template script setup import { ref } from vue; import uInput from /components/u-input/u-input.vue const user_phone ref() /script

相关新闻

C++并查集实现与优化:从原理到面试实战

C++并查集实现与优化:从原理到面试实战

1. 项目概述:为什么并查集是面试官的“心头好”?如果你刷过一些算法题,或者正在准备技术面试,大概率会碰到“朋友圈”、“岛屿数量”、“连通网络的操作次数”这类题目。乍一看,它们可能涉及图论、搜索,但老…

2026/11/1 6:42:14 阅读更多 →
Nodejs也能写Agent - 10.LangChain篇 - 初识LangChain

Nodejs也能写Agent - 10.LangChain篇 - 初识LangChain

我尝试了很多其它开发 Agent 的 Node.js 技术栈,兜兜转转一圈下来,我发现似乎只有 LangChain.js、LangGraph.js 才是最终的归途。 Mastra 很好,如果只是做独立的 Agent 确实不错,但如果要集成到自己的系统里面去,我觉得…

2026/11/2 1:08:49 阅读更多 →
现代C++数值计算:从经典算法到高性能工程实践

现代C++数值计算:从经典算法到高性能工程实践

1. 项目概述:为什么C依然是数值计算的基石聊到数值分析,很多人的第一反应可能是Python的NumPy、SciPy,或者MATLAB。确实,这些工具在快速原型、教学和数据分析领域有着无与伦比的优势。但如果你深入到高性能计算、工业级仿真、游戏…

2026/10/25 10:31:35 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/3 1:09:25 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/3 10:57:27 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/3 11:35:49 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/3 2:27:21 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/3 10:47:27 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/3 10:57:33 阅读更多 →

日新闻

周新闻

月新闻