未定义参考输入未定义 - @Input()在Angular 2中不起作用

Saa*_*zvi 3 typescript angular2-inputs angular

我是一个新手,试图从ng-book 2(v49)学习Angular2.以下是article.componenets.ts文件的内容:

import { Component, OnInit } from '@angular/core';
import { Article } from './article.model';

@Component({
  selector: 'app-article',
  templateUrl: './article.component.html',
 styleUrls: ['./article.component.css'],
 host: {
 class: 'row'
 }
})
export class ArticleComponent implements OnInit {
  @Input() article: Article;



  voteUp(): boolean {
    this.article.voteUp();
    return false;
  }

  voteDown(): boolean {
    this.article.voteDown();
    return false;
  }

  ngOnInit() {
  }

}
Run Code Online (Sandbox Code Playgroud)

这是angular-cli上的错误:

ERROR in C:/Users/saad/Desktop/books/Angular JS2/angular2Reddit/src/app/article/article.component.ts (13,4): Cannot find name 'Input'.)
webpack: Failed to compile.
Run Code Online (Sandbox Code Playgroud)

控制台错误

mic*_*dev 5

如果你想使用它,你必须导入输入:

import { Component, OnInit, Input } from '@angular/core';
Run Code Online (Sandbox Code Playgroud)


Ara*_*ind 5

您缺少Input指令的导入,因此将第一行更改为

import { Component, OnInit, Input } from '@angular/core';
Run Code Online (Sandbox Code Playgroud)

优良作法是让@Input参数具有某些值,否则您最终会在应用程序中的某些位置获得未处理的承诺错误.

为此,它可以在ngOnInit构造函数中定义

ngOnInit() {
   this.article={
         id: 0
         .....
   };
}
Run Code Online (Sandbox Code Playgroud)

要么

constructor(....) {
   this.article={
         id: 0,
         .....
   };
}
Run Code Online (Sandbox Code Playgroud)