webpack动态组件加载爬坑

为了动态配置组件,所以在AXIOS请求返回处理中做component = resolve => require([`${componentmp}.vue`], resolve)

这时候一直报错Cannot find module……at webpackEmptyContext(……)

多次测试,发现需要改成component = resolve => require([`@/${componentmp}.vue`], resolve)才可以

什么原因呢,在node手册里有提过,

require(X) from module at path Y

1. If X is a core module,

  a. return the core module

  b. STOP

2. If X begins with '/' 

  a. set Y to be the filesystem root

3. If X begins with './' or '/' or '../'

  a. LOAD_AS_FILE(Y + X)

  b. LOAD_AS_DIRECTORY(Y + X)

  c. THROW "not found"

4. If X begins with '#'

  a. LOAD_PACKAGE_IMPORTS(X, dirname(Y))

5. LOAD_PACKAGE_SELF(X, dirname(Y))

6. LOAD_NODE_MODULES(X, dirname(Y))

7. THROW "not found"

觉得翻译太吃力,竟然还真的有人翻译好了

我的第一种写法精准对上了not found的异常情况。

参考1:https://nodejs.org/api/modules.html#modules_all_together

参考2:http://www.ruanyifeng.com/blog/2015/05/require.html

你可能感兴趣的:(webpack动态组件加载爬坑)