17

I am trying to get @Input to work with Typescript in Angular 2. I am getting the following error and I don't understand why.

[ts] Cannot find name 'Input'. any

Below is the code from that component.

import { Component, OnInit } from '@angular/core';

@Component({
   selector: 'app-item',
   templateUrl: './app-item.component.html',
   styleUrls: ['./app-item.component.css']
})
export class AppItemComponent implements OnInit {
      @Input item; //TypeScript complains here. 

      constructor() { }

      ngOnInit() {}
}

The project and component were both created using the Angular CLI. Why can't TypeScript figure out the @Input decoration?

3 Answers 3

44

You need to add this,

import {Component, Input} from '@angular/core';
Sign up to request clarification or add additional context in comments.

2 Comments

That did it! Thanks!
Same for Output incase you are using that.
3

In my case I already had:

import { Component, OnInit } from '@angular/core'; 

So, when I tried to use:

import {Component, Input} from '@angular/core';

It didn't work.

What I had to do was import like that (Without the word Component)

import { Input } from '@angular/core';

And it worked just fine

Comments

3

@Marcielli, it didn't work because of the double import of Component. If you change the import statement to the following

import { Component, OnInit, Input } from '@angular/core';

You would be just fine. Optionally adding the Input module in a separate import statement is perfectly fine, but you should generally stick with either importing all components from a module in one statement, or each in separate statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.