角度:“意外令牌.构造函数,方法,访问器或属性是预期的. [英] Angular: "Unexpected token. A constructor, method, accessor or property was expected"

查看:70
本文介绍了角度:“意外令牌.构造函数,方法,访问器或属性是预期的.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么用varlet关键字声明变量时为什么会出现此编译错误?我的意思是,这很好:

I wonder why I get this compile error if I declare variable with var or let keywords? I mean, this goes well:

export class AppComponent {

    refreshClickStream$: any;

    constructor(){
    }

这会带来错误:

export class AppComponent {

    var refreshClickStream$: any;

    constructor(){
    }

推荐答案

在类中,TypeScript不允许使用

Inside a class, TypeScript doesn't permit the declaration of class members with

  • var
  • let
  • const (you can use readonly on the property)

此外,在类 ALSO 中,您将被禁止使用

Further, inside of a class ALSO you'll be prohibited from declarating functions with

  • function

所以你想要这个.

export class AppComponent {

  a: string = "foo";
  b: string = "bar";


  foo(): void { }

  constructor(){
  }

}

不是

export class AppComponent {

  var a: string = "foo";
  let b: string = "bar";


  function foo(): void { }

  constructor(){
  }

}

这篇关于角度:“意外令牌.构造函数,方法,访问器或属性是预期的.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆