【问题标题】:How to fix TypeError: application_module__WEBPACK_IMPORTED_MODULE_1___default.a is not a constructor如何修复 TypeError:application_module__WEBPACK_IMPORTED_MODULE_1___default.a 不是构造函数
【发布时间】:2019-03-29 16:17:35
【问题描述】:

我有创建类实例的代码,但发生错误

TypeError: application_module__WEBPACK_IMPORTED_MODULE_1___default.a 不是构造函数

index.ts

import Application from 'application-module';

const app = new Application();

应用程序模块

import { IApp, ModuleRoute, IReducerContainer } from 'common-module';

export default class Application implements IApp {
  routes: ModuleRoute[];
  reducers: IReducerContainer[];

  constructor() {
    this.routes = [];
    this.reducers = [];
  }

  addRoute(path: any, component: any) {
    this.routes.push({ path, component });
  }
}

common-module 只是一个实用类和接口的列表。

【问题讨论】:

  • 尝试在应用程序模块中删除default并将导入更改为import { Application } from 'application-module';
  • @PrzemyslawPietrzak 谢谢你,我试着用这种方式重写它,但得到了同样的错误
  • 没有default你得到TypeError: application_module__WEBPACK_IMPORTED_MODULE_1___default.a is not a constructo
  • @PrzemyslawPietrzak 没有默认我得到TypeError: application_module__WEBPACK_IMPORTED_MODULE_1__.Application is not a constructor
  • 其他:当你从源文件导入文件时,你应该使用相对路径:例如import Application from './application-module';请尝试以这种方式导入应用程序

标签: javascript typescript webpack


【解决方案1】:

application-modulepackage.json 中有主build/static/js/main-[hash].chunk.js 路径。为了解决我的问题,我们需要将其更改为index.js,但它会出现新错误:

Module parse failed: Unexpected token (10:25)
You may need an appropriate loader to handle this file type.
| import { enhancer } from './store/configureStore';
|
> export class Application implements IApp {

通过编辑webpack.config.js 来修复它,将ts-loader 中的include: paths.appSrc 更改为include: paths.appPath

之后webpack 可以看到我的node_modules,但是又出现了一个错误

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

附言

所以,我已经到了终点。我的 webpack 配置无法在src 目录之外编译.ts,我决定将.ts 代码编译为esnext 模块。如果现在连接模块,一切都会成功。

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多