我想在点击事件上记录名称 vale,但不能。 请指教。.ts 内部
export class ScrolldemoComponent implements OnInit {
constructor() { }
ngOnInit() {
const name = "test";
}
myEvent(event) {
console.log(name);
}
}
内部网页
<button (click)="myEvent()">My Button</button>
在组件级别使用它
name:string = 'test'
ngOnInit() {
this.name = "test";
}
myEvent(event) {
console.log(this.name);
}
"const" 创建块范围的变量,即它们只存在于围绕它们的最内层块中。
因此,您将不会获得"ngOnInIt"之外的名称值。请按照 Aravid 的建议在组件级别对其进行定义。
请向编译器将采用外部函数值的 oinit(name( 添加一些不同的值。它行不通