vue3+vite批量導出文件-使用import.meta代替require.context


對於webpack來說,可以使用require.context方法來實現文件的批量導出,但是vite搭建vue3項目時,不支持require,對於這種情況可以使用import.meta.glob或者import.meta.globEager來實現

二者使用方法相似,只是引入時機不同,globEager時立即引入,glob是異步引入

globEager

 const directives = import.meta.globEager('./*/index.js')
  for (let com in directives) {
    const comKey = com.match(/\.\/(.*?)\/index\.js/)[1]
    const comValue = directives[com].default
    app.directive(comKey, comValue)
  }

image-20210927164224060

glob

  const directives = import.meta.glob('./*/index.js')
  for (let dire in directives) {
    const direKey = dire.match(/\.\/(.*?)\/index\.js/)[1]
    directives[dire]().then((res) => {
      const direValue = res.default
      app.directive(direKey, direValue)
    })
  }

image-20210927164348127


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM