uniapp+vue3使用unplugin-auto-import实现自动引入vue及uniapp生命周期钩子
下载插件
pnpm install unplugin-auto-import
在vite.config.js中配置
import { defineConfig } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
import AutoImport from "unplugin-auto-import/vite";
export default ({}) => {
return defineConfig({
plugins: [
uni(),
AutoImport({
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/, // .vue
],
imports: ["vue", "uni-app"],
dts: "typings/auto-imports.d.ts",
}),
],
});
};
vue页面中无需引用即可使用
<script setup lang="ts">
const ref = (num = ref(null));
onLoad((opt) => {});
onShow(() => {});
</script>