prettier是代码格式化工具,Prettier只关心格式化文件(最大长度、混合标签和空格、引用样式等)
命令行格式化
快捷键Ctrl + Shift + P,上方出现命令框,搜索 “ format ” ,下面出现两种选择
- Format Document 对整个文档做格式化
- Format Selection 对选择代码做格式化
模版中“的干扰问题
比如下面模版语言中为了使用字符串,用了双引号,会导致prettier识别错误。
<div
id="flush-collapseTwo"
class="accordion-collapse collapse {{if eq .menu1 "analyze" }} show {{end}}}"
aria-labelledby="flush-headingTwo"
data-bs-parent="#accordionFlushConcept"
>
解决方法, 用``来标识字符串
参看https://beego.vip/docs/mvc/view/tutorial.md
{{"string"}} // 一般 string
{{`raw string`}} // 原始 string
{{'c'}} // byte
配置特定后缀的文件
vscode项目目录下新建个.prettierrc文件,内容如下:
{
"overrides": [
{
"files": "*.tpl",
"options": {
"parser": "html"
}
}
]
}