require.context用法

Posted little-ab

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了require.context用法相关的知识,希望对你有一定的参考价值。

let routerAll = require.context(‘../../modules‘, true, /.*/router/index.js$/), allRouter = []
console.dir(routerAll)

routerAll.keys().forEach(key => {
  console.dir(routerAll(key))
  if (routerAll(key).default.options.routes) {
    let temArr = routerAll(key).default.options.routes.reduce((pre, cur, index) => {
      pre = [...pre, {path: cur.path, name: cur.name}]
      return pre
    }, [])
    allRouter = allRouter.concat(temArr)
  }
})


export default allRouter

console.dir(routerAll)

技术图片

 

 有keys方法和resolve方法。

routerAll(key)可以获取到其中一个文件

技术图片

 

以上是关于require.context用法的主要内容,如果未能解决你的问题,请参考以下文章