Nuxt 3 使用 defineModel 报错:is a compiler-hint helper

发生背景

看着 Vue 3.3 新的编译器宏 defineModel 用着方便。

于是升级到了 Nuxt 3.6,想试用一下这颗新糖,但没仔细看 changelog 于是有了这篇文

相关代码

<script lang="ts" setup>

export interface Props {
  disabled?: boolean;
}

defineProps<Props>();
const value = defineModel<bigint>();

function handleInput(v: string) {
  if (v.startsWith('1')) value.value = BigInt(v);
  else value.value = 0n;
}

报错信息

[Vue warn]: defineModel() is a compiler-hint helper that is only usable inside <script setup> of a single file component. Its arguments should be compiled away and passing it at runtime has no effect. 

解决方案

nuxt.config.ts

 import viteLegacy from '@vitejs/plugin-legacy';
 export default defineNuxtConfig({
   modules: [
     '@nuxtjs/tailwindcss',
   ],
   vite: {
+    vue: {
+      script: {
+        defineModel: true,
+      },
+    },
     plugins: [
       viteLegacy({
         targets: ['chrome >= 77'],
       }),
     ],
   },
 });

发生原因

这是一个实验性功能。

但为什么警告是 is a compiler-hint helper 而不是 is a experimental feature,推测应该是 Nuxt 自动导入了 defineModel

但为什么需要手动重启开发服务器才能正常使用,有待斟酌。

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值