簡體   English   中英

Visual Studio Code'const'只能在.ts文件中使用

[英]Visual Studio Code 'const' can only be used in a .ts file

嘗試在Visual Studio Code中編寫基本JS時出現此錯誤。

已經嘗試更改settings.json($ workspace / .vscode / settings.json),但是它不起作用

  {
     "javascript.validate.enable": false
  }

在此處輸入圖片說明

您確定JavaScript語法正確嗎?

class Books {
    static const MAX_WIDTH = 8.5;
}

據我所知,即使在ES2015中也無法定義靜態屬性。

您可以嘗試其他方式,例如:

class Books {
    static get MAX_WIDTH() {
        return 8.5;
    }
}

console.log(Books.MAX_WIDTH);//8.5

Afaik,您不能在類聲明中定義靜態const。 你可以嘗試這樣的事情

 const MAX_WIDTH = 8.5; class Books { get MAX_WIDTH() { return MAX_WIDTH; } } let myBooks = new Books() alert(myBooks.MAX_WIDTH); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM