Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular 2/Typescript-TypeError:ClassName不是构造函数_Javascript_Angularjs_Typescript_Angular - Fatal编程技术网

Javascript Angular 2/Typescript-TypeError:ClassName不是构造函数

Javascript Angular 2/Typescript-TypeError:ClassName不是构造函数,javascript,angularjs,typescript,angular,Javascript,Angularjs,Typescript,Angular,我正在学习Angular 2,在尝试创建服务时遇到了这个错误。我试图寻找解决办法,但我看不出我的错误 错误: angular2-polyfills.js:1243 TypeError: Tweet is not a constructor export class TweetService{ getTweets(){ return tweets; } } let tweets = new Tweet("URL", "Author 1", "Handle 1",

我正在学习Angular 2,在尝试创建服务时遇到了这个错误。我试图寻找解决办法,但我看不出我的错误

错误:

angular2-polyfills.js:1243 TypeError: Tweet is not a constructor
export class TweetService{
    getTweets(){
        return tweets;
    }
}

let tweets = new Tweet("URL", "Author 1", "Handle 1", true, 50);

class Tweet {
    image: string;
    author: string;
    handle: string;
    status: "Lorem ipsum dolor sit amet.";
    isLiked: boolean;
    favorites: number;

    constructor(img, aut, hndl, ilkd, fav){
        img = this.image;
        aut = this.author;
        hndl = this.handle;
        ilkd = this.isLiked;
        fav = this.favorites;        
    }
}
代码:

angular2-polyfills.js:1243 TypeError: Tweet is not a constructor
export class TweetService{
    getTweets(){
        return tweets;
    }
}

let tweets = new Tweet("URL", "Author 1", "Handle 1", true, 50);

class Tweet {
    image: string;
    author: string;
    handle: string;
    status: "Lorem ipsum dolor sit amet.";
    isLiked: boolean;
    favorites: number;

    constructor(img, aut, hndl, ilkd, fav){
        img = this.image;
        aut = this.author;
        hndl = this.handle;
        ilkd = this.isLiked;
        fav = this.favorites;        
    }
}

let语句在类声明之外浮动。这是可行的(但在一个真正的应用程序中,你可以根据一些http呼叫或其他内容设置你的推文):


我已经在这里检查了你的代码,所有的编译都很好,但你有没有尝试导出Tweet类?@VasiliyVanchuk,这让人很奇怪。:)@奥利:我认为这不是问题所在。我试过了,但没修好。好吧,那我们至少可以排除这个问题。你说得对,先生。这解决了问题。关于http调用,我目前正在学习Angular 2和本地存储库更实用。一旦我变得更好,我将使用RESTful API。非常感谢。