首页 > 编程开发 > JavaScript    日期:2022-12-31 / 浏览

自动引入 composition api,不需要再手动引入。
github地址:https://github.com/antfu/unplugin-auto-import

下载

npm i unplugin-auto-import -D

修改vite.config.ts文件

import AutoImport from 'unplugin-auto-import/vite'
export default defineConfig({
  plugins: [vue(),AutoImport({
    imports: ['vue'],
    dts: 'src/auto-import.d.ts'
  })]
})

配置完毕后保存,此时会自动生成一个 auto-import.d.ts 的文件

vue3+vite引入插件unplugin-auto-import的方法

现在我们可以在项目中写入代码, 可以看到我们没有手动引入 ref 也可以在项目中正常使用。

<template>
    <div>
        <button @click="flag = !flag">change flag</button>
        <div>{{ flag }}</div>
    </div>
</template>
 
<script setup lang="ts">
const flag = ref(false);
</script>
 
<style lang="less"></style>

vue3+vite引入插件unplugin-auto-import的方法

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章