Typescript class.default 不是构造函数
Typescript class.default is not a constructor
创建一个 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';
}
}
会给我错误:原始异常:类型错误:second_class_1.default 不是构造函数
./二等内容
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'
代码中有一些错误:
-
导入时缺少
{}
-
调用构造函数时缺少
()
-
访问类成员时缺少
this
头等舱代码
import {SecondClass} from './second-class'
export class FirstClass {
someVar:string;
secondClass:SecondClass;
constructor(firstClass?: FirstClass) {
this.someVar='test';
this.secondClass= new SecondClass();
}
}
二等舱代码:
export class SecondClass {
someOtherVar:string;
constructor(secondClass?:SecondClass) {
this.someOtherVar='test';
}
}
这为时已晚,但我现在刚刚收到同样的错误。解决方案默认导出SecondClass
,因此第二类代码将为:
export default class SecondClass {
someOtherVar:string;
constructor(secondClass?:SecondClass) {
this.someOtherVar='test';
}
}
并使用import SecondClass from './second-class'
导入其他类
相关文章:
- ES6构造函数返回基类的实例
- 使用Google Visualization动态调用构造函数
- javascript中对象构造函数中的var属性与this.properties
- 理解typescript中的构造函数接口
- 为什么构造函数不是构造函数
- 如果在构造函数中有“返回”,则在 JavaScript 中的新运算符中做了什么
- 拦截对构造函数的调用
- 使用闭包共享构造函数参数
- 文本表示法VS.构造函数,用于在JavaScript中创建对象
- 从js引擎的角度来看闭包和构造函数是如何工作的
- 如何使用此从对象访问构造函数
- Javascript:为什么是构造函数's __proto__属性Empty(){}
- 当一个重要的构造函数参数丢失时应该发生什么
- Chrome Javascript日期构造函数错误
- 如何使用构造函数's的输出,以便将值插入到对象中
- 为什么在调用父构造函数时在[]中发送数据
- 构造函数函数闭包变量
- ES6类是否与构造函数相同
- 构造函数中的事件处理程序与构造函数外的事件处理函数的行为不同
- Typescript class.default 不是构造函数