简体   繁体   English

VS Code - 如何从 angular html 模板创建方法?

[英]VS Code - How to create a method from angular html template?

So, when I want to declare a new method in html of Angular component, WebStorm provides me opportunity to create this method in ts:因此,当我想在 Angular 组件的 html 中声明一个新方法时,WebStorm 为我提供了在 ts 中创建此方法的机会: 示例网络风暴

But, if I try same at VS code I can't create a method.但是,如果我在 VS 代码中尝试相同的方法,我将无法创建方法。 Is there some solution?有什么解决办法吗? (extensions, maybe something in settings) (扩展,也许在设置中) 示例代码

You can use the extension My Code Actions v0.6.0.您可以使用扩展程序My Code Actions v0.6.0。

Add this to your settings.json将其添加到您的settings.json

"my-code-actions.actions": {
    "[html]": {
      "Create function {{atCursor:$1}}": {
        "file": "${fileBasenameNoExtension}.ts",
        "atCursor": "\\(click\\)=\"([^\"]+)\\(\\)\"",
        "text": "function {{atCursor:$1}}() {\n}\n\n"
      }
    }
  }

You can add more complex (multiple) edits if needed如果需要,您可以添加更复杂(多个)的编辑

You can have a look at the doc page of the extension to see an example of a complex edit to add / modify an import statement.您可以查看扩展的文档页面,查看添加/修改导入语句的复杂编辑示例。

Adjust the languageID if Angular template files are not html.如果 Angular 模板文件不是 html,请调整languageID

This is now possible with the VSCode Angular Language Service plugin v14.2.0现在可以使用VSCode Angular 语言服务插件v14.2.0

vscode 角度代码操作

So, when I want to declare a new method in html of Angular component, webstorm provides me opportunity to create this method in ts:因此,当我想在 Angular 组件的 html 中声明一个新方法时,webstorm 为我提供了在 ts 中创建此方法的机会: 示例网络风暴

But, if I try same at VS code I can't create a method.但是,如果我在 VS 代码上尝试相同,我将无法创建方法。 Is there some solution?有什么解决办法吗? (extensions, maybe something in settings) (扩展,可能在设置中) 示例 vscode

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

相关问题 VS Code 未指示 Angular 模板中不存在该方法 - VS Code not indicating that method does not exist in Angular template 如何将代码片段添加到 Angular html 模板? - How to add code snippet to Angular html template? 如何在 Angular 中使用模板文字和 *ngFor 创建动态 html 模板 - How to create a dynamic html template with template literals and *ngFor in Angular VS Code:如何在 Angular 模板文件中重构(重命名变量)? - VS Code: How to refactor(rename variable) in an Angular template file? Angular 4将值从html模板传递到组件中的方法 - Angular 4 passing a value from html template to a method in a component 如何从组件中的方法创建和分配任意Angular2本地模板变量 - How to create and assign an arbitrary Angular2 local template variable from a method in my component 如何从VS Code调试Angular 6子项目 - How to debug Angular 6 subproject from VS Code 如何使用角形材料组件创建HTML电子邮件模板 - How to create Html email template with angular material component 如何在 angular 服务中创建 html 模板并将其用作 htmlTemplateRef? - how to create html template in angular service and use it as a htmlTemplateRef? Angular - 如何在 VS Code 的 html 文件中更改变量的颜色? - Angular - How to change variable's color in html file in VS Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM