简体   繁体   English

Angular :意外的令牌。 需要构造函数、方法、访问器或属性

[英]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.

相关问题 意外的标记。 预期构造函数,方法,访问器或属性的角度为5 - Unexpected token. A constructor, method, accessor, or property was expected angular 5 问:错误:意外的令牌。 需要构造函数、方法、访问器或属性。ts(1068) - Q: Error: Unexpected token. A constructor, method, accessor, or property was expected.ts(1068) TypeScript 意外标记,需要构造函数、方法、访问器或属性 - TypeScript Unexpected token, A constructor, method, accessor or property was expected TypeScript:意外的令牌; '构造函数,函数,访问器或变量' - TypeScript : Unexpected token; 'constructor, function, accessor or variable' reactJS 意外令牌,应为“;” 在构造函数中 - reactJS Unexpected token, expected “;” in constructor TypeScript- NodeJS-意外令牌; “构造函数,函数,访问器或变量” - TypeScript- NodeJS - Unexpected token; 'constructor, function, accessor or variable' React Native 构造函数:意外的标记,预期的“;” - React Native constructor: Unexpected token, expected “;” 语法错误:意外的标记。 异步/等待节点 8 - SyntaxError: Unexpected token. async/await node 8 一个新对象抛出“意外令牌”。 - a new object throw “Unexpected token.” nodejs React组件构造函数(解析错误:意外令牌,期望“}”) - React component constructor (Parsing error: Unexpected token , expected “}”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM