Pac*_*cky 7 vue.js vue-component
是否可以在组件的样式标签中使用变量?基本上,我已经将mixin导入到我的样式标签中,该标签接受2种颜色以在类中创建渐变。它很好用,但是我希望它具有动态性,因此可以通过数据库进行设置。我知道我可以通过内联来绑定样式,但是div的渐变相当长,并且作为mixin效果更好。
零件:
<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/mixins";
.color-section{
color:red;
@include diagonalGradient( content.gradient_color_one , content.gradient_color_two);
}
</style>
Run Code Online (Sandbox Code Playgroud)
混合蛋白
@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 );
}
Run Code Online (Sandbox Code Playgroud)
Mac*_*sch 14
v-bind在 Vue 3 中,您现在可以在单个文件组件<style>部分中使用,如下所示:
<style scoped>
.color-section {
color: v-bind('sectionColor');
}
</style>
Run Code Online (Sandbox Code Playgroud)
您应该使用计算属性,因为它们可能是实现您想要做的事情的最佳和最干净的方法。他们在 Vue 文档上还有一个关于它的完整网站:
https://v2.vuejs.org/v2/guide/class-and-style.html
基本上,你可以这样做:
computed: {
style () {
return 'background: ' + this.top + ';' + '...'
}
}
Run Code Online (Sandbox Code Playgroud)
您可以传递顶部和底部变量,而不是传递 mixin。这非常方便,因为在计算样式 () 函数中,您可以自由地执行任何您想要的与 javascript 相关的操作,因此您可以使用条件、表达式等。让您能够强大地控制自己的风格;)
| 归档时间: |
|
| 查看次数: |
10307 次 |
| 最近记录: |