gpt4 book ai didi

javascript - Typescript class.default 不是构造函数

转载 作者:数据小太阳 更新时间:2023-10-29 04:01:52 30 4
gpt4 key购买 nike

创建 Angular2 应用程序时,在第一类的构造函数中调用另一个类的构造函数时,我遇到了以下问题。

一级代码

import SecondClass from './second-class'

export class FirstClass {
someVar:string;
secondClass:SecondClass;

constructor(firstClass?: FirstClass) {
someVar='test';
secondClass= new SecondClass;
}
}

二等舱代码:

export class SecondClass {
someOtherVar:string;

constructor(secondClass?:SecondClass) {
someOtherVar='test';
}
}

会给我错误:ORIGINAL EXCEPTION: TypeError: second_class_1.default is not a constructor

./second-class的内容

System.register([], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var SecondClass;
return {
setters:[],
execute: function() {
SecondClass = (function () {
function SecondClass(secondClass) {
this.someOtherVar='test';
}
return SecondClass;
}());
exports_1("SecondClass", SecondClass);
}
}
});
//# sourceMappingURL=second-class.js.map

这是 Typescript 编译器的编译输出

最佳答案

错误消息暗示您在 ./second-class 中使用了命名导出(export class SecondClass {})(非默认)。所以这意味着你的导入应该看起来像

import {SecondClass} from './second-class'

关于javascript - Typescript class.default 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829342/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com