简体   繁体   English

VS 代码没有将关键字“this”链接到当前的 javascript object?

[英]VS Code not linking keyword “this” to current javascript object?

In VS Code, i'm making a js file with this piece of code在 VS Code 中,我正在用这段代码制作一个 js 文件

const ObjectX = {
      abc(){

      },
      xyz(){
           this.abc()
      },
}

And when I'm typing this.当我输入this. and press Ctrl + space I expect VS Code to suggest me with abc() as a member of this but it doesn't.并按Ctrl + space我希望 VS Code 建议我使用abc()作为其中的成员, this事实并非如此。

When I type the full line this.abc() then press F2 at definition line to rename abc() , VS Code doesn't rename all the calls to this.abc() as well.当我键入整行this.abc()然后在定义行按F2重命名abc()时,VS Code 也不会重命名对this.abc()的所有调用。

All 2 facts definitely say that VS Code doesn't link the keyword this to current object.所有 2 个事实都明确表明 VS Code 没有将关键字this链接到当前的 object。 This behavior is inconvenient cause another install of VS on another PC do links.这种行为很不方便,因为在另一台 PC 上再次安装 VS 会产生链接。

Anyone experienced this can give me a line of configuration to fix VS Code?任何经历过这种情况的人都可以给我一行配置来修复 VS Code 吗?


PS1: This is just bad coding experience with VS Code, not the way javascript code works! PS1:这只是 VS Code 的糟糕编码体验,而不是 javascript 代码的工作方式!

PS2: I've tried TypeScript with the same behavior. PS2:我试过 TypeScript 具有相同的行为。

PS3: I have intellisense with newest version of VSCode and my jsconfig.json looks like this PS3:我有最新版本的 VSCode 的智能感知,我的jsconfig.json看起来像这样

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "experimentalDecorators": true
    },
    "exclude": [
        "node_modules",
        ".vscode",
        "library",
        "local",
        "settings",
        "temp"
    ]
}

What is your file extension?你的文件扩展名是什么? If it is.js it should intellisense this.abc() as javascript.如果是.js,它应该将 this.abc() 智能感知为 javascript。

What does the language mode show when the file is open under VS.Code.在 VS.Code 下打开文件时语言模式显示什么。 Language mode is displayed at the bottom right corner.语言模式显示在右下角。 If it is not showing "JavaScript" as language mode you can click on it and change to JavaScript.如果它没有显示“JavaScript”作为语言模式,您可以单击它并更改为 JavaScript。 Default is AutoDetect.默认为自动检测。 You can also type Ctrl-K and then M keys to bring up language mode.您也可以按 Ctrl-K,然后按 M 键调出语言模式。

See here: https://code.visualstudio.com/docs/languages/overview#_changing-the-language-for-the-selected-file见这里: https://code.visualstudio.com/docs/languages/overview#_sharing-the-language-for-the-selected-file

The answer is simple but hard to find out:答案很简单,但很难找到:

Add this line to tsconfig.json of project将此行添加到项目的tsconfig.json

  "compilerOptions": {
     ....
    "noImplicitThis": true
     ....
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM