简体   繁体   English

Vue 样式化本地组件

[英]Vue styling local components

I have Single-File Components and I want to use it in different places like subscribe page and footer.我有单文件组件,我想在订阅页面和页脚等不同的地方使用它。 But I want to style to be different.但我想风格不同。 Is it possible to style local components?是否可以设置本地组件的样式?

Component file: Newsletter.vue组件文件:Newsletter.vue

<template>
<form>
    <label>Enter your email address here</label>
      <input type="text" name="email" required/>
</form>
  <button @click="subscribe" >
    SUBSCRIBE
  </button>
</template>

Subscribe page: Subscribe.vue订阅页面:Subscribe.vue

let say style Newsletter background-color green and align to left让我们说风格时事通讯背景色绿色并左对齐

<template>
 <h2>
  Subscribe to receive Newsletter
  </h2>
  <Newsletter></Newsletter>
</template>

<script>
import Newsletter from "@/components/Newsletter.vue";

export default {
 components: {
  Newsletter
 }
}
</script>

<style scoped>
 Newsletter{
  background-color: green;
  text-align:left;
 }
</style>

Subscribe page: Footer.vue订阅页面:Footer.vue

but in Footer, I want to style Newsletter background-color red and align center但是在页脚中,我想将 Newsletter 背景色设置为红色并居中对齐

   <template>
      <h2>
        JOIN OUR NEWSLETTER
      </h2>
      <Newsletter></Newsletter>
   </template>

<script scoped>
import Newsletter from "@/components/Newsletter.vue";

export default {
 components: {
  Newsletter
 }
}
</script>

<style>
 Newsletter{
  background-color: red;
  text-align:center;
 }
</style>

This is possible!这个有可能! All you have to do is add the scoped attribute to the style tag .您所要做的就是将scoped 属性添加到 style 标签

Here is the snippet from the docs just for reference here:这是文档中的片段,仅供参考:

<template>
  <button class="btn btn-close">X</button>
</template>

<style scoped>
.btn-close {
  background-color: red;
}
</style>

This will only apply the styles locally, to the component which contains the style tag.这只会在本地将 styles 应用于包含样式标签的组件。 Keep in mind though, you really should avoid using tag selectors because they are the most generic.但请记住,您确实应该避免使用标签选择器,因为它们是最通用的。 It's a better practice to use a class selector because it has a higher specificity, meaning it is less likely to be over-written (id selectors or inline styles take precedence).最好使用 class 选择器,因为它具有更高的特异性,这意味着它不太可能被覆盖(id 选择器或内联 styles 优先)。 I would suggest doing something like this:我建议做这样的事情:

<template>
 <h2>
  Subscribe to receive Newsletter
  </h2>
  <Newsletter class="newsletter"></Newsletter>
</template>

<script>
import Newsletter from "@/components/Newsletter.vue";

export default {
 components: {
  Newsletter
 }
}
</script>

<style scoped>
 .newsletter {
  background-color: green;
  text-align:left;
 }
</style>

In the example above, the newsletter class will make the background-color green only in this single-file component.在上面的示例中, newsletter class 将在此单文件组件中将背景颜色设为绿色。

There are many options.有很多选择。 You may benefit from using the ::v-deep combinator which allows leaking the style to the child components.您可能会受益于使用::v-deep组合器,它允许将样式泄漏给子组件。

Using your example with a small change:使用您的示例进行小改动:

<template>
 <h2>
  Subscribe to receive Newsletter
  </h2>
  <Newsletter class="newsletter"></Newsletter>
</template>

<script>
import Newsletter from "@/components/Newsletter.vue";

export default {
 components: {
  Newsletter
 }
}
</script>

<!-- this is the change. Updated to have `::v-deep` to allow style to "leak" to the child component -->
<style scoped>
 ::v-deep .newsletter {
  background-color: green;
  text-align:left;
 }
</style>

See more at:更多信息请访问:

https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors

Another option to consider is using global style in place of the local style and be sure that each shared class element has a unique parent.要考虑的另一个选项是使用全局样式代替本地样式,并确保每个共享的 class 元素都有一个唯一的父元素。

In one component such as Subscribe:在订阅等一个组件中:

<style lang="scss">
 .subscribe { 
  .newsletter {
   // style for this specific version of newsletter
  }
}
</style>

In another component where ".somethingelse" should be the specific type of component:在另一个组件中,“.somethingelse”应该是组件的特定类型:

<style lang="scss">
 .somethingelse {
   .newsletter {
     // style for this other specific version of newsletter
   }
 }
</style>

More info about global specificity:有关全局特异性的更多信息:

https://developer.mozilla.org/en-US/docs/Web/CSS/Specificityhttps://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM