vue3+ts+vite项目使用 unplugin-auto-import (自动导入)

该项目是基于vite创建的 vue3 + ts + vue-router + pinia项目;

启动项目:

复制代码
npm install
npm run dev

启动后遇到了以下问题:

问题1:

Component name "Person" should always be multi-word. eslint(vue/multi-word-component-names)

解释:组件名"Person"应该总是由多个单词组成(组件名 name: 'Person')

解决方法:
.eslintrc.cjs 中添加以下配置

复制代码
rules: {
  'vue/multi-word-component-names': 'off'
}

问题2:

项目中会频繁引入使用 vue、vue-router、pinia 内的API, 如ref、reactive

使用自动引入插件进行全局引入即可

使用步骤:

  • 安装:npm i -D unplugin-auto-import
  • 修改 vite.config.ts
javascript 复制代码
import AutoImport from 'unplugin-auto-import/vite'

export default defineConfig({
  plugins: [
    // ... other
    AutoImport({
      imports: ['vue','vue-router', 'pinia'], // 自动引入的三方库
      // dts: 'src/auto-import.d.ts', // 全局自动引入文件存放路径;不配置保存在根目录下;配置为false时将不会生成 auto-imports.d.ts 文件(不影响效果)
      eslintrc: {
        // 项目中使用了 eslint,那么虽然可以正常使用 API 了,但是 eslint 还是会报没有引入的报错。下面的配置可以处理这种情况
        enabled: true, // 会在根目录下自动生成 .eslintrc-auto-import.json 文件
      }
    })
  ]
})
  • .eslintrc.cjs 中使用自动生成的 .eslintrc-auto-import.json 文件

    module.exports = {
    root: true,
    extends: [
    // ... other 配置
    './.eslintrc-auto-import.json'
    ],
    }

ps: 我配置了后并没有效果,还是会报错如找不到名称"ref"。ts-plugin(2304),不知道为啥!

有知道的大佬说一声呢,感谢!!()

文章仅为本人学习过程的一个记录,仅供参考,如有问题,欢迎指出!

相关推荐
京东云开发者6 小时前
【全栈实践】第一个 AI Agent 项目:从零搭建 AI 音频创作助手(入门篇)
typescript·agent·vuex
燐妤9 小时前
中老年智能健康管理项目
python·ai·pycharm·vue3·fastapi·项目开发·健康
王林不想说话10 小时前
React + Ant Design 后台项目:63 个业务组件的分层实践
react.js·typescript·ant design
大家的林语冰11 小时前
👍 超越 ESLint,Oxc 优先采用 TypeScript 7,Rust 和 Go 梦幻联动!
前端·javascript·typescript
embedded大铭13 小时前
ai时代上站记录
typescript
用户938515635071 天前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformers.js 全链路实战(三)
前端·react.js·typescript
执子念的飞鱼1 天前
File Viewer 进入 2K 节点后,我更在意这 3 条回归
前端·typescript
mONESY1 天前
TypeScript 面试高频:type 与 interface 到底怎么选?
typescript
meilindehuzi_a1 天前
TypeScript面试题:interface 与 type:相同点、核心区别与选择指南
java·ubuntu·typescript
用户938515635071 天前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformer.js 全链路实战(二)
前端·javascript·typescript