You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vuejs 3.3中defineModel始终返回undefined,求排查解决

解决Nuxt 3.5.1 + Vue 3.3中defineModel返回undefined的问题

可能的原因及修复步骤:

  • 修正defineModel的使用方式:
    Vue 3.3中启用defineModel后,无需传递字符串参数(该参数应为配置对象,而非prop名称)。你的代码里传'count'是错误用法,修改为:

    <template>
        <input v-model="count">
    </template>
    
    <script lang="ts" setup>
      const count = defineModel();
    
      // 若需自定义prop名称或修饰符,可传入配置对象,示例:
      // const count = defineModel({ name: 'count' })
    </script>
    
  • 调整Nuxt配置的正确位置:
    你当前把Vue的script配置嵌套在vite选项下,Nuxt无法正确识别。将配置移到顶层的vue选项中:

    // nuxt.config.ts
    export default defineNuxtConfig({
      vue: {
        script: {
          defineModel: true,
          propsDestructure: true
        }
      }
    })
    
  • 清除Nuxt缓存并重启项目:
    配置修改后,旧缓存可能导致配置不生效,执行以下命令清除缓存:

    npx nuxi clean
    

    之后重新启动开发服务器。

  • 验证Vue版本兼容性:
    检查package.json中的vue版本号,确保确实是3.3+。若版本不符,执行npm install vue@3.3.x更新依赖。

内容的提问来源于stack exchange,提问作者aym1781969

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:52:11