如何在 VSCode 中为 Typescript 生成 Getter 和 Setter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50960053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:29:21  来源:igfitidea点击:

How to generate Getter and Setter for Typescript in VSCode

typescriptvisual-studio-code

提问by Daniel Eduardo Delgado Diaz

I'm used to using webstorm to generate getter and setter for classes.

我习惯使用 webstorm 为类生成 getter 和 setter。

Is there any way to do it with VSCode in short steps?

有什么方法可以在短时间内用 VSCode 做到这一点吗?

回答by John Archer

This feature is shipped builtin with Visual Studio Code 1.24 (May 2018); see here: https://code.visualstudio.com/updates/v1_24#_generate-gettersetter-refactoring

此功能内置于 Visual Studio Code 1.24(2018 年 5 月)中;见这里:https: //code.visualstudio.com/updates/v1_24#_generate-gettersetter-refactoring

Basically you select the line with the property, then a Quick Action bulb is shown which you have to click. Then you can generate the getters/setters.

基本上,您选择带有属性的行,然后会显示一个快速操作灯泡,您必须单击它。然后你可以生成getter/setter。

回答by Olofu Mark

The shortcut is Ctrl+Shift+Rafter highlighting the property.

高亮属性后快捷键是Ctrl+Shift+R

Longer method: Right click on the property. Then click on refactor from the popup menu.

更长的方法:右键单击该属性。然后从弹出菜单中单击重构。

Currently, vscode has no feature to mass generate getters and setters for all class properties at once like in other IDEs so it has to be done one property at a time.

目前,vscode 没有像其他 IDE 那样一次性为所有类属性批量生成 getter 和 setter 的功能,因此必须一次完成一个属性。

回答by Uday Chauhan

To generate getter and setters in typescript :

在打字稿中生成 getter 和 setter:

1)Download and install the TypeScript Toolbox extension in vscode. https://marketplace.visualstudio.com/items?itemName=DSKWRK.vscode-generate-getter-setter?

1)在vscode中下载并安装TypeScript Toolbox扩展。 https://marketplace.visualstudio.com/items?itemName=DSKWRK.vscode-generate-getter-setter?

2)Press Ctrl+Shift+X or Cmd+Shift+X to open the Extensions viewlet. Find the Go extension, click on it to open the Extension Editor.

2) 按 Ctrl+Shift+X 或 Cmd+Shift+X 打开扩展视图。找到 Go 扩展,点击它打开扩展编辑器。

3) You can view the Visual Studio Code settings for the Go extension along with their default values and description in the product itself. enter image description here

3) 您可以在产品本身中查看 Go 扩展的 Visual Studio Code 设置及其默认值和描述。 在此处输入图片说明

4) tick the option for Use 'getValue' and 'setValue' instead of 'get value' and 'set value'.

4) 勾选 Use 'getValue' 和 'setValue' 而不是 'get value' 和 'set value' 的选项。

enter image description here

在此处输入图片说明

How to use :

如何使用 :

1) Press Ctrl+Shift+P or Cmd+Shift+P

1) 按 Ctrl+Shift+P 或 Cmd+Shift+P

2) Enter and search for command Generate All Getter and Setter then hit Enter.

2) 输入并搜索命令 Generate All Getter 和 Setter,然后按 Enter。

enter image description here

在此处输入图片说明

回答by Lynx 242

May someone correct me who knows it better, but as far as I know the bare VS Code is not capable of providing this convenience. I'm sure that there exist certain plugins you can use, but I don't know any name.

可能有人更了解我,但据我所知,裸露的 VS Code 无法提供这种便利。我确定您可以使用某些插件,但我不知道任何名称。

Edit

编辑

Maybe TypeScript Toolboxis what you are searching for.

也许TypeScript Toolbox就是您要搜索的内容。

回答by Ishara Amarasekera

You can install this extension to Visual Studio Code to generate getters and setters. https://marketplace.visualstudio.com/items?itemName=afmicc.GetterAndSetterGenerator

您可以将此扩展安装到 Visual Studio Code 以生成 getter 和 setter。 https://marketplace.visualstudio.com/items?itemName=afmicc.GetterAndSetterGenerator

Hope this helps.

希望这可以帮助。

回答by Palash Toshniwal

write propin service file or any .ts file and hit enter!

在服务文件或任何 .ts 文件中写入prop并按回车键!