是否可以将变量与组件的样式标记一起使用?基本上,我已经在我的样式标签中导入了一个mixin,它接受两种 colored颜色 ,在一个类中创建一个渐变.它工作得很好,但我想要这个动态,所以我可以通过数据库设置它.我知道我可以通过内联绑定样式,但div的渐变相当长,作为混音效果更好.

组成部分:

<template>

    <section class="section" v-bind:class=" { 'color-section' : content.gradient_color_one }">

        <div class="container">

            <div class="columns">

                <div class="column is-half">

                    <h2 class="is-size-4" v-html="content.title"></h2>

                    <div class="section-content" v-html="content.description"></div>

                    <a class="button" :href=" '/'+ content.button_link">{{ content.button_text }}</a>

                </div>

                <div class="column">

                    <img :src="content.image" :alt="content.title" />

                </div>

            </div>

        </div>

    </section>

</template>

<script>
    export default {

        props:[
            'content'
        ],

    }
</script>

<style lang="scss" scoped>

    @import "../../sass/helpers/混入";

    .color-section{
        color:red;
        @include diagonalGradient( content.gradient_color_one , content.gradient_color_two);
    }

</style>

混入

@mixin diagonalGradient($top, $bottom){
  background: $top;
  background: -moz-linear-gradient(-45deg, $top 0%, $bottom 100%);
  background: -webkit-gradient(left top, right bottom, color-stop(0%, $top), color-stop(100%, $bottom));
  background: -webkit-linear-gradient(-45deg, $top 0%, $bottom 100%);
  background: -o-linear-gradient(-45deg, $top 0%, $bottom 100%);
  background: -ms-linear-gradient(-45deg, $top 0%, $bottom 100%);
  background: linear-gradient(135deg, $top 0%, $bottom 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#92fe9d', endColorstr='#00c8ff', GradientType=1 );
}

推荐答案

你应该使用计算(computed)属性,因为它们可能是实现你想要做的事情的最好、最干净的方式.

https://vuejs.org/v2/guide/class-and-style.html

基本上,你可以这样做:

computed: {
  style () {
    return 'background: ' + this.top + ';' + '...'
  }
}

然后可以传递顶部和底部变量,而不是传递mixin.这非常方便,因为在computed style()函数中,您可以自由地执行任何与javascript相关的操作,因此可以使用条件、表达式等等.让你对自己的风格有强有力的控制;)

Vue.js相关问答推荐

为什么 Vue 在将设置脚本中的计算(computed)属性渲染到模板时找不到变量

Vue 3 / Vuetify 3:如何为按钮设置全局默认 colored颜色

如何在 VUE.js 中重新刷新时保持多层 b-collapse 菜单打开?

在 Ajax 函数中访问 Vue.js 组件属性

Rails Webpacker 还是 Vue-CLI?

云Firestore保存额外的用户数据

VueJS:如何在组件数据属性中引用 this.$store

如何在 vuejs 自定义指令中传递动态参数

Vue Table 2 - 自定义过滤器

在下拉菜单上使用箭头键滚动

未捕获的类型错误:无法读取未定义的属性initializeApp

自定义props类型

如何从我的 Vuetify 数据表中正确更新 Vuex 状态

错误:vue-loader 要求 @vue/compiler-sfc 存在于依赖树中

Javascript/vue.js 接收json

v-if inside v-for - 在两列中显示项目列表

如何在 JEST 测试中模拟全局 Vue.js 变量

克隆元素并附加到 DOM 的正确方法

Vue.JS 2.0 修改不相关数据时速度慢

如何在 Vue.js 2 应用程序中模拟 onbeforeunload?