Emma Hoffman
Emma Hoffman

Reputation: 71

@Input() value not defined in Angular 2

I'm trying to get isActivated from an @Input() tweets. But When I want to store the isActivated to a variable this error comes up: Tweet is not defined. and Here's my code:( And sorry for my English too)

export class SingleTweetComponent { 
    @Input() tweet;
    isActivated = tweet.isActivated;
    likeButton($event){
        this.tweet.totalLikes = this.tweet.totalLikes - 1;
    }
}

Upvotes: 1

Views: 697

Answers (1)

Wojciech Kwiatek
Wojciech Kwiatek

Reputation: 6802

Because tweet is not defined when you're trying to use it. At the time of computing properties you don't have input data yet.

To fix that you can take it in OnInit hook of the component:

export class SingleTweetComponent { 
    @Input() tweet;

    ngOnInit() {
      this.isActivated = this.tweet.isActivated;
    }

    likeButton($event) {
      this.tweet.totalLikes = this.tweet.totalLikes - 1;
    }
}

Upvotes: 3

Related Questions