Ste*_*n-v 7 javascript dynamic-import webpack vue.js vue-component
我目前正在使用require.context加载所有.vue文件名不以Async.
const loadComponents = (Vue) => {
    const components = require.context('@/components', true, /\/[A-Z](?!\w*Async\.vue$)\w+\.vue$/);
    components.keys().forEach((filePath) => {
        const component = components(filePath);
        const componentName = path.basename(filePath, '.vue');
        // Dynamically register the component.
        Vue.component(componentName, component);
    });
};
现在我想加载以Asyncwith结尾的组件,require.context这样我就不必在创建这种类型的新组件时手动添加它们。
通常动态导入语法如下所示:
Vue.component('search-dropdown', () => import('./search/SearchDropdownAsync'));
这将通过承诺解决并动态导入组件。
出现的问题是,当您使用 时require.context,它会立即加载(需要)组件,而我无法使用动态导入。
有什么办法可以结合require.contextWebpack的动态导入语法吗?
https://webpack.js.org/guides/code-splitting/#dynamic-imports
有第四个论据require.context可以帮助解决这个问题。
https://webpack.js.org/api/module-methods/#requirecontext
https://github.com/webpack/webpack/blob/9560af5545/lib/ContextModule.js#L14
const components = require.context('@/components', true, /[A-Z]\w+\.(vue)$/, 'lazy');
components.keys().forEach(filePath => {
  // load the component
  components(filePath).then(module => {
    // module.default is the vue component
    console.log(module.default);
  });
});
| 归档时间: | 
 | 
| 查看次数: | 7240 次 | 
| 最近记录: |