Typescript class.default 不是构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36829342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:25:49  来源:igfitidea点击:

Typescript class.default is not a constructor

javascripttypescript

提问by Nandan Phadke

Creating an Angular2 app, I am facing the following problem, when calling the constructor of another class inside the constructor of first class.

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

First Class code

一级代码

import SecondClass from './second-class'

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

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

Second Class code:

二级代码:

export class SecondClass {
    someOtherVar:string;

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

Would give me the error: ORIGINAL EXCEPTION: TypeError: second_class_1.default is not a constructor

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

Content of ./second-class

./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

This is the compiled output from Typescript compiler

这是 Typescript 编译器的编译输出

回答by dfsq

Error message implies that you used named export (export class SecondClass {}) in ./second-class(not default). So it means that your import should looks something like

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

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

回答by tibbus

There are some errors in the code :

代码中有一些错误:

  • missing {}from import

  • missing ()from calling the constructor

  • missing thisfrom accessing Class members

  • {}导入时丢失

  • 缺少()调用构造函数

  • 缺少this访问类成员

First Class code

一级代码

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

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

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

Second Class code:

二级代码:

export class SecondClass {
    someOtherVar:string;

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

回答by Timo

this is too late, but I just got the same error right now. Solution is export SecondClassas default so Second Class code will be:

这太晚了,但我现在遇到了同样的错误。解决方案是SecondClass默认导出,因此二等代码将是:

export default class SecondClass {
    someOtherVar:string;

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

and import in other class with import SecondClass from './second-class'

并在其他类中导入 import SecondClass from './second-class'