简体   繁体   English

Vuejs 中的动态导入

[英]dynamic imports in Vuejs

I am using server-side rendered vue for my application (not bootstrapped using vue-cli).我正在为我的应用程序使用服务器端渲染的 vue(不是使用 vue-cli 引导的)。 And my application currently registers all the vue components globally using Vue.component() in the index.js file which is loaded in the base.html of my server.我的应用程序目前在 index.js 文件中使用 Vue.component() 全局注册所有 vue 组件,该文件加载到我的服务器的 base.html 中。

I need to perform code-splitting, in order to load only the required components as per the url path and to do that I am using the dynamic import syntax for loading the components.我需要执行代码拆分,以便根据 url 路径仅加载所需的组件,为此我使用动态导入语法来加载组件。

Vue.component('component-a', () => import(/* webpackChunkName: "component-a" */ './component-a.vue'))

I get the error below (currently loading the components on http://localhost:8000/dashboard/ ) it tries to search for the chunk in /dashboard/<chunk.js> like this:我收到以下错误(当前在http://localhost:8000/dashboard/上加载组件)它尝试在/dashboard/<chunk.js>中搜索块,如下所示:

GET http://localhost:8000/dashboard/component-a.tmp2hr7_bhp.js net::ERR_ABORTED 404 (Not Found)

and this和这个

[Vue warn]: Failed to resolve async component: () => __webpack_require__.e(/*! import() | component-a */ "component-a").then(__webpack_require__.bind(null, /*! ./component-a.vue */ "./dashboard/static/dashboard/scripts/component-a"))
Reason: ChunkLoadError: Loading chunk component-a failed.
(error: http://localhost:8000/dashboard/component-a.tmp2hr7_bhp.js)

I tested this using a basic vue-cli bootstapped app without any custom webpack config, it worked just fine.我使用没有任何自定义 webpack 配置的基本 vue-cli 引导应用程序对此进行了测试,它工作得很好。 not sure why this causes a problem here also tried the babel plugin-syntax-dynamic-import but didn't work.不知道为什么这会导致问题在这里也尝试了 babel plugin-syntax-dynamic-import但没有奏效。

Not sure, is there a path problem (as it appends the file chuck name to the url path), since, webpack is not able to find the chunk js file... thanks for the help!不确定,是否存在路径问题(因为它将文件卡盘名称附加到 url 路径),因为 webpack 无法找到块 js 文件...感谢您的帮助!

I think its a path problem.我认为这是一个路径问题。

Try setting alias in your webpack config (this is what vue-cli uses by default):尝试在 webpack 配置中设置别名(这是 vue-cli 默认使用的):

 alias: {
      '@': path.resolve(__dirname, 'yourRootFolder')
    }

now use @ in the component path like this.现在像这样在组件路径中使用@。 Vue.component('component-a', () => import(/* webpackChunkName: "component-a" */ '@/component-a.vue')) Vue.component('component-a', () => import(/* webpackChunkName: "component-a" */ '@/component-a.vue'))

This is correct assuming the component is in the same directory:假设组件位于同一目录中,这是正确的:

Vue.component('component-a', () => import(/* webpackChunkName: "component-a" */ './component-a.vue'))

The filename in your webpack.config.js's output property's file name is created dynamically which will have [name].[chunkhash].js you can change it to [name].js as mentioned below.您的 webpack.config.js 的output属性的文件名中的文件名是动态创建的,它将具有[name].[chunkhash].js您可以将其更改为[name].js ,如下所述。

output: {
  filename: '[name].js',
  ...
},

I think this will solve the issue.我认为这将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM