v-deep 在 Vue.js 中算是反模式吗?

huangapple go评论69阅读模式
英文:

Is v-deep an anti-pattern in Vue.js?

问题

When styling a Vue.js component, you can use ::v-deep to target CSS classes of nested components, e.g.

<template>
  <div class="parent">
    <Child /> <!-- contains .grandchild CSS class -->
  </div>
</template>

// ...

<style lang="scss" scoped>
.parent {
  ::v-deep {
    .grandchild {
      display: block;
    }
  }
}
</style>

由于重命名或删除 .grandchild 可能会影响 Child 的使用者,所以使用 v-deep 是否被认为是不良做法?

英文:

When styling a Vue.js component, you can use ::v-deep to target CSS classes of nested components, e.g.

<template>
  <div class="parent">
    <Child /> <!-- contains .grandchild CSS class -->
  </div>
</template>

// ...

<style lang="scss" scoped>
.parent {
  ::v-deep {
    .grandchild {
      display: block;
    }
  }
}
</style>

Given that renaming or removing .grandchild could break consumers of Child, isn't using v-deep considered bad practice?

答案1

得分: 3

这取决于上下文,它在你处理无法直接修改的组件时非常重要,例如第三方组件。在其他情况下,最好遵循最佳实践,比如使用 props 来管理子组件的样式,或者使用全局 CSS。

因此,::v-deep 应该只在没有其他选择时作为最后的手段使用。

英文:

It depends on context, it can be esential in cases where you are working with components that you cannot modify directly, e.g. third party components.
In the rest of cases is better to follow best practices such as using props to manage child component styles or using global CSS.

For that reason ::v-deep should be only used as a last resort when there are no other options.

huangapple
  • 本文由 发表于 2023年5月22日 13:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定