[英]Angular : unexpected token. A constructor, method, accessor, or property was expected
when I try to run my code when I declare a property with var or let keywords I get an error,当我使用 var 或 let 关键字声明属性时尝试运行我的代码时,出现错误,
this goes fine:这很好:
export class productComponent {
productId: any;
constructor(){
}
And this show the error:这显示了错误:
export class productComponent {
var productId: any;
constructor(){
}
doesn't typescript and javascript uses var and let to declare variables and angular is based on typescript.不打字稿,javascript 使用 var 和 let 声明变量,角度基于打字稿。 I know that someone will say productId is a field and not a variable , but isn't a field a variable ?
我知道有人会说 productId 是一个字段而不是变量,但字段不是变量吗? what are the differences between field and variable in this case ?
在这种情况下,字段和变量之间有什么区别?
When you declare a class in TypeScript for example:例如,当您在 TypeScript 中声明一个类时:
class Program{ public nombre: string; getNombre(){ return this.nombre; } setNombre(nombre:string){ this.nombre = nombre; } }
That's the correct syntax, it's incorrect to use the var/let/const keyword to declare a property inside a class or an object.这是正确的语法,使用 var/let/const 关键字在类或对象中声明属性是不正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.