【问题标题】:Cannot find name 'Input'.ts(2304) [closed]找不到名称'Input'.ts(2304)[关闭]
【发布时间】:2019-12-25 17:34:36
【问题描述】:

我想使用角度数据绑定和 Input 装饰器将输入值从一个组件获取到另一个给定的子组件。

我有一个最小的例子来告诉你我想如何完成这个任务:

父模板:

<h4>Passing value of input element to child component:</h4>
<input [ngModel]="name" (ngModelChange)="name = $event">
<p>{{name}}</p>
<hello [name]="name"></hello>

父组件:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = '';
}

子组件:

@Component({
  selector: 'hello',
  template: `<h4>Passed : "{{ name }}" to child component!</h4>`,
  styles: [`h1 { font-family: Lato; }`]
})
export class HelloComponent  {
  @Input() name: string;
}

这似乎在 stackblitz 上运行良好: https://stackblitz.com/edit/angular-pggcbd

但我的本地打字稿编译器响应:

Cannot find name 'Input'.ts(2304)

并标记@Input() 装饰器。有什么想法吗?

【问题讨论】:

  • 您可能导入了错误的类。
  • 为了更好地澄清问题,请添加带有导入声明的完整文件。
  • 您的 snippet 对我来说很好用,或者缺少一些东西

标签: angular typescript decorator


【解决方案1】:

只是缺少或不正确的导入。

将导入添加到import { Component, Input } from '@angular/core'

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 2020-06-24
    • 2018-01-04
    • 2018-07-08
    • 2018-04-19
    • 2021-01-27
    相关资源
    最近更新 更多