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

Vue3 SFC中ESLint误报props未使用的合规解决方法

解决Vue3 <script setup>中ESLint no-unused-vars 误报问题

这里有三种可行的解决方法,都不需要禁用规则或添加忽略注释:

  • 显式引用props属性
    ESLint目前无法自动关联<script setup>里的props变量和模板中的使用,我们可以在脚本里显式引用一次props.msg,哪怕只是用void做无意义调用,就能消除误报:

    <template>
        <div>{{msg}}</div>
    </template>
    
    <script setup>
        const props = defineProps({
            msg: {
                type: String,
                default: 'Hello world'
            }
        })
        // 显式引用消除ESLint误报
        void props.msg;
    </script>
    
  • 解构方式获取props
    直接解构defineProps的返回值,这样ESLint能识别到解构出的msg变量被模板使用,不会触发未使用警告:

    <template>
        <div>{{msg}}</div>
    </template>
    
    <script setup>
        const { msg } = defineProps({
            msg: {
                type: String,
                default: 'Hello world'
            }
        })
    </script>
    
  • 配置Vue专属ESLint插件(推荐)
    安装eslint-plugin-vue并在ESLint配置中启用Vue3的规则集,它能正确识别<script setup>的语法特性,从根源避免这类误报:

    {
      "extends": [
        "plugin:vue/vue3-essential"
      ],
      "plugins": [
        "vue"
      ]
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 23:25:17