Vue3 defineProps` / `defineEmits` 是编译器宏,不需要手动 import 导入

警告解读

复制代码
`defineEmits` is a compiler macro and no longer needs to be imported.
`defineProps` is a compiler macro and no longer needs to be imported.

翻译:defineProps / defineEmits 是编译器宏,不需要手动 import 导入。

哪里会触发警告

在<script setup>里面写了下面这种错误代码:

复制代码
<script setup>
// ❌错误!不要import
import { defineProps, defineEmits } from 'vue'

const props = defineProps({ ... })
const emit = defineEmits(['xxx'])
</script>

Vue3 <script setup>环境下,defineProps、defineEmits是编译器自动注入的全局宏,直接写,不需要 import。

✅正确写法

复制代码
<script setup>
// ✅删掉 import { defineProps, defineEmits } from 'vue'
const props = defineProps({
  imgSrc:{
    type:[String,Object],
    default:''
  }
})
const emit = defineEmits(['update:modelValue'])
</script>

排查步骤

  1. 在项目全局搜索 import { defineProps,找到写了这句的 vue 组件,把这两个从 import 里面删掉。
  2. 重启 pnpm dev,终端黄色警告就消失。

顺带看你的 useErrorView.js

复制代码
import { computed } from 'vue'
import defaultImg from '@/assets/images/error/img_404.png'

export function useErrorView(props) {
  const finalImgSrc = computed(() => {
    return props.imgSrc || defaultImg
  })

  return {
    finalImgSrc
  }
}

调用示例(vue 组件)

复制代码
<script setup>
import { useErrorView } from '@/composables/useErrorView'

const props = defineProps({
  imgSrc: {
    type: [String, Object],
    default: ''
  }
})

const { finalImgSrc } = useErrorView(props)
</script>

注意:必须把组件的props作为参数传给 composable 函数 ,不能在 composable 内部直接defineProps,defineProps只能在 vue 单文件顶层使用。

小补充

类似的宏还有:defineOptions、defineModel,全部都不要 import。

终端那个局域网地址 http://172.26.208.1:3981/ 是 vite 暴露给局域网其他设备访问的地址,属于正常输出。

相关推荐
掘金挖土5 小时前
前端手摸手跑路之 AI 应用开发(九)
前端·后端
中文呀7 小时前
三维 GIS 离线缓存实战:逆向 CesiumOfflineCache,并亲手为它补上"自动更新"能力
前端
前进前进前进8 小时前
TRAE 全链路实战:做了一个警示牌一键生成器:纯 Canvas、单文件、零依赖
前端
小万7429 小时前
God‘s Eye View 深度解析:15 路公开信号 + CesiumJS 的浏览器级 OSINT 控制台,60 帧渲染的“真实间谍卫星模拟器”
前端
IT星宿9 小时前
浏览器标签页全是一个图标:我用名字哈希生成 favicon,让每个项目都有身份标识
前端
OpsEye9 小时前
大模型账单对账难:不同厂商 Token 统计口径差异怎么处理
javascript·ai编程
全栈项目管理程序猿9 小时前
ArcGIS JS 基础教程(32):体元系列 - VoxelSlice 体元切片
前端·javascript
拖孩11 小时前
AI 帮我写的小程序「厨菜记」上线满月了,挣了两百块!!!
前端·后端·微信小程序
用户3600555790011 小时前
Kestrel_VLLM - 13 · 为什么排除 MoE FFN Tiling 与 Tensor Core
前端
流烟默12 小时前
SSE 实战:在 SpringBoot + Vue 项目中零依赖实现轻量级实时消息推送
vue.js·spring boot·后端·sse