【发布时间】:2018-11-16 08:05:18
【问题描述】:
我一定遗漏了一些关于 @Input 的信息,因为我从 Angular CLI 收到了这个错误:
ERROR in app/web/progress/progress-button/progress-button.component.ts(10,4): Error during template compile of 'ProgressButtonComponent'
Only initialized variables and constants can be referenced in decorators because the value of this variable is needed by the template compiler in 'Input'
'Input' is not initialized at ../@angular/core/src/metadata/directives.ts(855,22).
我的模板包含这个:
<button class="btn btn-primary btn-progress"
(click)="startProgress($event)"
[myInput]="foobar">TEST
</button>
打字稿包含以下内容:
import { Component, OnInit } from '@angular/core';
import { Input } from '@angular/core/src/metadata/directives';
@Component({
selector: 'app-progress-button',
templateUrl: './progress-button.component.html',
styles: []
})
export class ProgressButtonComponent implements OnInit {
@Input() myInput: string;
constructor() {}
ngOnInit() {}
startProgress(event) {}
}
我在这里缺少什么?
更新
根据以下建议,我相信我发现了我的错误:
<app-progress-button [myInput]="'foobar'"></app-progress-button>
在组件中:
<button class="btn btn-primary btn-progress"
(click)="startProgress($event)">{{myInput}}
</button>
【问题讨论】:
-
我不明白,你为什么在同一个组件中使用输入?你不应该在另一个组件中使用它,然后通过它的标记标签调用它,就像
<my-component [myInput]="'foobar'" > </my-component> -
我想你可能就在这里,我的意思是在父级中添加输入,如
<app-progress-button [myInput]="foobar"></app-progress-button>