zorroe

zorroe

Using the unplugin-auto-import/vite plugin in Vue 3.

Automatic Import

Installation#

npm i -D unplugin-auto-import

Configuration#

  1. Configure in vite.config.ts
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      imports: ["vue"],
      dts: true,
    }),
  ],
});

After setting dts:true, a auto-imports.d.ts file will be generated in the current path.

Note: You can also specify a path.

  1. Configure in tsconfig.json
"include": [
    "src/**/*.ts",
    "src/**/*.d.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "./**.d.ts"   *****This line
],

The purpose of this configuration is to prevent red lines in vue files, although it does not affect the execution.

Effect#

<template>
  <div>
    <button @click="num += 1">+</button>
  </div>
  <div>
    {{ num }}
  </div>
</template>

<script setup lang="ts">

const num = ref(30);

</script>

image-20230722223218900

image-20230722223305801

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.