javascript - 如何使用 import() 在代码拆分中导入一个类?

标签 javascript reactjs performance code-splitting transloadit

使用代码拆分后,我找不到如何在我的 React 应用程序中导入类。

之前(有效!):

import React, {PureComponent} from 'react';
import Tus from './components/test';

 class Shopper extends PureComponent {
    constructor (props) {
    super(props); }

  uploadModal () { 

/* some function code... */

   .use(Tus, { 
        endpoint: 'http://192.168.22.124:3000/upload/'
    })
  /* more codes... */ 
 }

使用代码拆分后(不起作用):

import React, {PureComponent} from 'react';

 class Shopper extends PureComponent {   
    constructor (props) {
    super(props); }

  uploadModal () { 

/* some function code... */

   .use(import('./components/test').then(Tus => Tus, { 
        endpoint: 'http://192.168.22.124:3000/upload/'
    })
  /* more codes... */ 
 }

使用代码拆分后出现此错误

TypeError: Expected a plugin class, but got object. Please verify that the plugin was imported and spelled correctly.

当我控制台时

import('./component/test').then(Tus => console.log(Tus))

我明白了:

ƒ Tus(uppy, opts) {
    _classCallCheck(this, Tus);

    var _this = _possibleConstructorReturn(this, _Plugin.call(this, uppy, opts));

    _this.type = 'uploader';
    _this.id = 'Tus';
    _this.titl…

最佳答案

在您的工作示例中(在代码拆分之前),您似乎正在从“./components/test”导入默认导出。

动态导入以允许代码拆分后,您应该使用Tus.default 来获得相同的结果。您可以在 webpack code splitting documentation 上阅读更多相关信息.

换句话说,import('./component/test').then(Tus => Tus.default)

希望对您有所帮助!干杯!

关于javascript - 如何使用 import() 在代码拆分中导入一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52114890/

相关文章:

javascript - jQuery:如何选择表格中除每行最后一个单元格之外的每个单元格?

javascript - 如果我从 JS 调用 SWF,而该 SWF 已经忙于处理来自 JS 的另一个调用,会发生什么情况?

javascript - 使用 i18next 将变量替换为 ReactNode 元素

android - RecyclerView onMeasure 性能问题

python - 如何将驻留在内存中的数据结构从一个模块传递到另一个模块

javascript - HTML Canvas 标签的内容是否会自动创建为 PNG 文件?

javascript - 在 View 中动态插入部分及其在 Angular 中的数据

javascript - 无法使用 jQuery 和 React.js 加载 data.json 文件

reactjs - 如何使用 React 为映射函数中的各个元素设置状态?

python - 在python中获得一个范围的所有排列而无需连续邻居的最快方法