VUE脚本标签中的更漂亮的破坏功能

发布于 2025-01-31 10:40:33 字数 864 浏览 0 评论 0 原文

我想将IF功能保留在一行中,而无需使用三元格式,但更漂亮的格式破坏了它,我在Prettier formitier文档中没有找到该选项

.vue 文件
想要的输出

<script>
export default {
  methods: {
    changeSlide(slideIndex) {
      if (slideIndex >= this.slides.length) { slideIndex = 0 }
    }
  },       
}
</script>

我的Prettier

<script>
export default {
  methods: {
    changeSlide(slideIndex) {
      if (slideIndex >= this.slides.length) {
        slideIndex = 0
      }
    }
  },       
}
</script>

我正在使用NUXT(VUEJS)

Config:

{
  "semi": false,
  "singleQuote": true,
  "tabWidth": 4,
  "useTabs": true,
  "printWidth": 120
}

I would like to keep a if function in one line without using ternary if but prettier format breaks it, I didn't find a option for that in prettier documentation

tag script inside .vue file
WANTED OUTPUT

<script>
export default {
  methods: {
    changeSlide(slideIndex) {
      if (slideIndex >= this.slides.length) { slideIndex = 0 }
    }
  },       
}
</script>

prettier format

<script>
export default {
  methods: {
    changeSlide(slideIndex) {
      if (slideIndex >= this.slides.length) {
        slideIndex = 0
      }
    }
  },       
}
</script>

I'm working with Nuxt(VueJS)

my prettier config:

{
  "semi": false,
  "singleQuote": true,
  "tabWidth": 4,
  "useTabs": true,
  "printWidth": 120
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2025-02-07 10:40:33

您使用的是带有VSCODE扩展名或通过ESLINT的更漂亮的?我建议第二种方法顺便说一句,对于nice linter linter + formatter combo 。
这还将为您提供
禁用权利 linting +格式 +格式 + formatting + formatting + formatting + formatting 代码,总体上更加灵活。


如果您使用的是第一种方法,则可以尝试此范围忽略< /a>方法。

您可以使用 // prettier-ignore-ignore 忽略整个事情,但我怀疑您可以在之后启用它。如您在选项中,没有什么可以帮助那里的选项。但是,要漂亮的目的是被认为是可以自以为是的,因此合法的是,您没有Eslint可以带来的全部灵活性。

有一些准则,例如一个“ nofollow Noreferrer”>以特定的写作方式。打败了更漂亮的目的:使用它,并停止一劳永逸地讨论每种拉动请求上的样式,因此为什么它具有自以为是,并且没有那么多可配置的选项。

一个简单的(但 meh hack)将是设置“ printWidth”:200 ,这可能以某种方式工作,但并不能真正灵活。


TLDR:使用Eslint + Prettier组合(无VSCODE扩展)进行完全露面的柔性配置,或者让Prettier Do tos to and To Blitier Do The Condect的格式。

You're using Prettier with a VScode extension or via ESlint? I recommend the second approach btw, for a nice linter + formatter combo.
This will also give you the right to disable linting + formatting of a specific line or block of code, so quite more flexiible overall.


If you're using the first approach, then you could maybe try this Range ignore approach.

You could use // prettier-ignore to ignore the whole thing but I doubt you can enable it back afterwards. As you saw in the options, there is nothing that can help there. But the purpose of Prettier is to be opinionated so it's legit that you don't have the whole flexibility that ESlint could bring to the table.

There are some guidelines like the one from Airbnb, this is great but then you need your whole team to agree on a specific way of writing things. Which defeats the purpose of Prettier: use it and stop discussing the style on each Pull Request once and for all, hence why it's opinionated and have not that much configurable options.

A simple (yet meh hack) would be to set "printWidth": 200, this may somehow work but not really flexible.


TLDR: use an ESlint + Prettier combo (without the VScode extension) for a fully fledged flexible configuration or let Prettier do it's opinionated formatting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文