Principle:
Before preloading, the plug-in automatically imports on demand Yes, use api and components
in this vue file and when writing code, justNo need to import
Note that it is not a global import and will not affect the resources
Ⅰ. Comparison before and after use
Before using the plug-in:
<script setup> import { ref } from "@vue/reactivity"; import { useRouter } from "vue-router"; const router = useRouter(); const name = ref('张三'); </script>
After using the plug-in:
<script setup> const router = useRouter(); const name = ref('张三'); </script>
Ⅱ. Install third-party software
npm i -D unplugin-auto-import
Ⅲ. Configure third-party software
##vite.config configuration:
import { defineConfig } from "vite"; import AutoImport from 'unplugin-auto-import/vite' export default defineConfig({ plugins: [ AutoImport({ imports: ['vue', 'vue-router'] }), ] //......... })
Ⅰ. Comparison before and after use
Use Before the plug-in:
<template> <div class="main"> <Aside /> <Footer /> </div> </template> <script setup> import Aside from '/@/components/Aside.vue' import Footer from '/@/components/Footer.vue' </script>
<template> <div class="main"> <Aside /> <Footer /> </div> </template> <script setup></script>
Ⅱ. Install third-party software
You can set up components to be imported on demand, or you can set up UI frameworks to be imported on demand (such as: element plus, Antd... : Set the UI framework to load automatically. Be careful not to import the UI framework into main.js. When packaging at the same time, use as many UI components as you want to package.npm i -D unplugin-vue-components
The above is the detailed content of How to use vue3 api automatic import plug-in. For more information, please follow other related articles on the PHP Chinese website!