我想在一行中保留一个if函数,而不使用三元if,但更漂亮的格式会 destruct 它,我在更漂亮的文档中找不到这样的选项

tag script inside .vue file
WANTED OUTPUT

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

更漂亮的格式

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

我正在与Nuxt(VueJS)合作

我的漂亮配置:

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

推荐答案

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.


如果您使用的是第一种方法,那么您可以try this Range ignore种方法.

你可以用// prettier-ignore来忽略整件事,但我怀疑你以后能否启用它.正如你在the options中所看到的,没有什么能帮到你.但是,Prettier的目的是固执己见,所以你没有ESlint所能带来的全部灵活性是合法的.

有一些指导原则,比如from Airbnb条,这很好,但是你需要你的整个团队就具体的写作方式达成一致.这与"更漂亮"的目的背道而驰:使用它,一劳永逸地停止讨论每个Pull请求的样式,因此它是自以为是的,没有那么多可配置的选项.

一个简单的方法(meh破解)是设置"printWidth": 200,这可能会在某种程度上起作用,但实际上并不灵活.


TLDR:使用ESlint+Prettier组合(不带VScode扩展)来实现完全灵活的配置,或者让Prettier执行其自以为是的格式.

Vue.js相关问答推荐

点击屏幕的任何位置都会禁用v-overlay

Vue composition api: 访问