在下面的组件类中,如果我添加 let
关键字,代码将无法编译。为什么?
export class HelloComponent {
@Input() name: string;
name2:string;//let name2:string doesn't compile.
constructor(){
}
}
最佳答案
let
和 var
只需要/允许局部变量,而不是类字段。
export class HelloComponent {
@Input() name: string;
name2:string;//let name2:string doesn't compile.
constructor(){
var x = 5; // ok
let y = 5; // ok
const z = 5; // ok
}
}
在方法(或构造函数)之外的类中,只允许变量初始化和方法声明,因此 let
是多余的,因此是不允许的。
关于angular - 为什么添加关键字 `let` 或 `var` 会使代码无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47810656/