Reputation: 2272
I am learning Angular 2 and I ran into this error while trying to create a service. I tried searching for a solution, but I cannot see my mistake.
Error:
angular2-polyfills.js:1243 TypeError: Tweet is not a constructor
Code:
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;
}
}
Upvotes: 2
Views: 7346
Reputation: 5015
Your let statement is floating outside the class declarations. This will work (but in an real app you would be setting your tweets based on some http call or something):
import {Injectable} from '@angular/core';
@Injectable()
export class TweetService{
getTweets(){
let tweets = new Tweet("URL", "Author 1", "Handle 1", true, 50);
return tweets;
}
}
class Tweet {
image: string;
author: string;
handle: string;
status: "Lorem ipsum dolor sit amet.";
isLiked: boolean;
favorites: number;
constructor(img, aut, hndl, ilkd, fav){
this.image = img;
this.author = aut;
this.handle = hndl;
this.isLiked = ilkd;
this.favorites = fav;
}
}
Upvotes: 2