首页 > 其他分享 >unplugin-auto-import 工程项目 import 模块自动导入

unplugin-auto-import 工程项目 import 模块自动导入

时间:2024-05-07 10:58:23浏览次数:18  
标签:Vue auto 导入 unplugin API import

渡一(袁老师)视频详解[点击前往]

unplugin-auto-import 是一个用于** Vue 3**(和 Vue 2Composition API)的插件,它可以自动导入你在代码中使用的 Vue Composition API 函数(如 ref, reactive, computed 等)以及来自其他库的函数(如 Vue RouteruseRoute, useRouter,或者 PiniadefineStore 等)。

这个插件的主要目标是减少样板代码和提高开发效率。当你开始使用** Vue 3** 的 Composition API 或者其他现代库时,你可能会发现你需要在每个组件中导入许多函数。这可能会导致代码变得冗长和难以阅读。unplugin-auto-import 通过自动检测你的代码并为你导入必要的函数来解决这个问题。

如何使用 unplugin-auto-import
安装:
使用 npmyarn 安装 unplugin-auto-import

bash
npm install unplugin-auto-import --save-dev

或者

yarn add unplugin-auto-import --dev

配置:

在你的 Vite、Vue CLI、Rollup 或其他构建工具的配置文件中,配置 unplugin-auto-import。以下是一个 Vite 的例子:

// javascript
// vite.config.js  
import { defineConfig } from 'vite'  
import AutoImport from 'unplugin-auto-import/vite'  
  
export default defineConfig({  
  plugins: [  
    AutoImport({  
      // 你可以指定要自动导入的库  
      imports: [  
        'vue',  
        'vue-router',  
        // ...其他库  
      ],  
      // 你可以指定要排除的导入  
      eslint: {  
        // 如果你正在使用 ESLint,这个选项将生成相应的 ESLint 规则  
        inheritEnv: true,  
        // 如果你想禁用 ESLint 规则,可以设置为 false  
        // disabled: true,  
      },  
    }),  
    // ...其他插件  
  ],  
})

在代码中使用:
现在,你可以在你的 Vue 组件中直接使用 Vue Composition API 的函数,而无需显式导入它们。

// vue
<template>  
  <div>{{ count }}</div>  
  <button @click="increment">Increment</button>  
</template>  
  
<script setup>  
// 不需要显式导入 ref 或 onMounted  
const count = ref(0)  
const increment = () => {  
  count.value++  
}  
</script>

注意:unplugin-auto-import 还支持 TypeScript,并且可以与 ESLint 集成,以在你不小心显式导入已自动导入的函数时发出警告。

标签:Vue,auto,导入,unplugin,API,import
From: https://www.cnblogs.com/bing23443414/p/18176704

相关文章

  • Unveiling the Mercedes C6: A Masterpiece of Automotive Ardour
    WelcometoourblogpostdedicatedtotheextraordinaryMercedesC6,theepitomeofautomotiveardour.Inthisarticle,wewilldelveintothecaptivatingfeatures,unrivaledperformance,andtimelesseleganceofthisexceptionalluxuryvehicle.Whethery......
  • autoware学习(一)autoware概述
    简介代码结构├──autoware#主要源代码├──car_demo#主要与仿真相关├──citysim#主要与仿真相关├──drivers#不需要太关注,主要与上车测试相关└──vendor#不需要太关注,主要与上车测试相关├──autoware│  ├──common#通用功能│  ├─......
  • Only a type can be imported. XXX resolves to a package
    在编写jsp页面是,导入需要的包,运行时报错main.jsp<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><%@pageimport="java.util.List"%><%@pageimport="com.beans.E......
  • AutoCAD C# 两不平行直线倒圆弧算法
    参考的博客:https://www.cnblogs.com/JJBox/p/14300098.html下面是计算示例主要计算代码:varpeo=newPromptEntityOptions("选择直线1"){AllowNone=false,AllowObjectOnLockedLayer=false};peo.SetRejectMessage("请选择直线Line");p......
  • Nessus 10.7 Auto Installer for RHEL 9/AlmaLinux 9/Rocky Linux 9 (updated Apr 202
    Nessus10.7AutoInstallerforRHEL9/AlmaLinux9/RockyLinux9(updatedApr2024)发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu22.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-rhel-9/,查看最新版。原创作品,转载请保留出处......
  • Nessus 10.7 Auto Installer for macOS Sonoma (updated Apr 2024)
    Nessus10.7AutoInstallerformacOSSonoma(updatedApr2024)发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu22.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-macos/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org......
  • oh-my-posh,Import-Module爆红
    描述搞oh-my-posh的时候,一直没注意看报错内容是啥,以为是教程不对(实际上确实不对,因为过期了),后面才发现,它写的是现在已经不再支持PowerShellModule。于是点了链接,到官网照着做,马上就搞好了。https://ohmyposh.dev/docs/migrating大致流程就是,卸载模块安装的oh-my-posh,改为其他的......
  • [AutoCAD] cad虚线显示的是实线
    在CAD软件中,虚线显示为实线的问题通常是由线型比例设置不当导致的。线型比例是指虚线中直线段和空段的长度比例,当线型比例设置得非常大或非常小时,虚线可能会显示为实线。例如,如果全局比例因子设置得非常高,那么虚线中的直线段和空段长度会变得非常小,以至于在屏幕上显示为......
  • Python之禅,开宗明义:import this
    #!/usr/bin/envpythonimportthisPython之禅:TheZenofPython,byTimPetersBeautifulisbetterthanugly.Explicitisbetterthanimplicit.Simpleisbetterthancomplex.Complexisbetterthancomplicated.Flatisbetterthannested.Sparseisbette......
  • 使用新版flask-script时报错No module named flask._compat和cannot import name ‘_r
    flask版本:3.0.3Flask-Script:2.0.6Flask-script使用及错误Nomodulenamedflask._compat解决方法windows下推荐解决方案,点击flask_script进入init.py文件或虚拟环境\Lib\site-packages\flask_script_init_.pylinux下cd到目录/usr/local/lib/python3.12/site-packages......