js-import 动态导入模块

const loadData  = (name)=>{
  return new Promise (resolve=>{
    // 动态导入 不能有中文名
    import('./xyjd.js').then((data)=>{
      console.log('data--->', data)
      resolve(data.default)
    })
  })
}


const init = async ()=>{
  const result = await loadData()
  console.log('result--->', result)
}

init()

注意:动态导入的文件名不能为中文

// xyjd.js

const a = {
    xx:'ff'
}

export default a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unplugin-auto-import 是一个用于自动导入模块的 Vite 插件,可以帮助我们快速导入组件、函数、变量等等。以下是在 Vue 3 + TypeScript 项目中使用 unplugin-auto-import 自定义导入路径的步骤: 1. 安装所需依赖: ```bash npm install -D vite-plugin-auto-import ``` 2. 在构建配置中添加插件,并配置自定义导入: ```javascript import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import AutoImport from 'unplugin-auto-import/vite' export default defineConfig({ plugins: [ vue(), AutoImport({ // 配置需要自动导入模块 imports: [ { // 导入组件 path: 'vue', // 定义导入的名称 imports: ['defineComponent'], }, { // 导入自定义组件 path: '@/components/MyCustomComponent.vue', // 定义导入的名称 imports: ['MyCustomComponent'], }, { // 导入函数 path: 'lodash/debounce', // 定义导入的名称 imports: ['debounce'], }, { // 导入变量 path: '@/config', // 定义导入的名称 imports: ['appConfig'], }, ], }), ], }) ``` 3. 在代码中使用自定义导入模块: ```vue <template> <MyCustomComponent /> </template> <script lang="ts"> import { defineComponent } from 'vue' import { debounce } from 'lodash' import { appConfig } from '@/config' import { MyCustomComponent } from '@/components/MyCustomComponent.vue' export default defineComponent({ components: { MyCustomComponent, }, data() { return { searchText: '', } }, methods: { search: debounce(() => { console.log('searchText:', this.searchText) }, 500), }, }) </script> ``` 注意:在使用 unplugin-auto-import 插件时,需要在 tsconfig.json 文件中开启 "esModuleInterop": true,以支持模块导入

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值