【问题标题】:How to add the following styles as props for this Vue component如何添加以下样式作为此 Vue 组件的道具
【发布时间】:2020-05-06 07:13:07
【问题描述】:

如何添加以下样式作为组件的 props?

        <template>
        <div>
            <transition-group name='fade' tag='div'>
             ...
          </div>
        </template>

        <script>
        export default {
          name: 'Slider',
          data() {
            return {

            }
      props: { intervalVal: Number,
               width...
 },
          },
        </script>

    styles>

div {
width: 100% 
height: 600px
}

.prev,
.next {
color #fffff
background-color:#22222

我怎样才能把 div 和 height 作为 props 和 prev。 .next 类颜色和背景颜色?

【问题讨论】:

    标签: vue.js vuejs2 vue-component


    【解决方案1】:

    当您只想使用道具设置标签样式时,我会这样做

    <div :style="`width: ${this.width}`">
    

    如果你想动态添加 css 类,你可以像 here 描述的那样使用对象

    在你的模板中

    <div v-bind:class="{ active: isActive }"></div>
    

    在你的组件中

    data: {
      isActive: true,
      hasError: false
    }
    

    你也可以使用计算的属性来动态地做到这一点

    【讨论】:

    • 您解释的第一个选项是否比仅覆盖导入此组件的组件中的默认样式更好?
    • 第一个选项可用于传入(或计算)特定样式的动态值。
    猜你喜欢
    • 2022-08-24
    • 2023-03-16
    • 2021-07-05
    • 1970-01-01
    • 2020-02-02
    • 2022-06-18
    • 2021-03-25
    • 2018-09-23
    • 2018-10-19
    相关资源
    最近更新 更多