我有几种需要应用于文本的不同样式。我正在尝试使用数组语法来绑定样式,如文档https://vuejs.org/v2/guide/class-and-style.html所示,但不确定我在做什么错。
我创建了一支用于演示的笔:https://codepen.io/anon/pen/orVGNP 计算的属性样式也是我尝试应用的一种,它会更改字体样式和字体粗细。
<div id="colorpicker">
<v-layout justify-center>
<v-flex class="ml-5">
<chrome-picker v-model="colors"></chrome-picker>
</v-flex>
<v-flex>
<chrome-picker v-model="colors1"></chrome-picker>
</v-flex>
</v-layout>
<v-container>
<v-layout justify-center>
<v-btn-toggle v-model="btnTgl" class="ma-2" multiple>
<v-btn>
<v-icon>format_bold</v-icon>
</v-btn>
<v-btn>
<v-icon>format_italic</v-icon>
</v-btn>
<v-btn>
<v-icon>format_underlined</v-icon>
</v-btn>
<v-btn>
<v-icon>maximize</v-icon>
</v-btn>
</v-btn-toggle>
<v-flex xs6 class="ml-5">
</v-flex>
</v-layout>
<div v-bind:style="[{ color: colors.hex, background:colors1.hex, style
}]" class="title">
Random Test Text!!!!!
</div>
</v-container>
</div>
var Chrome = window.VueColor.Chrome;
new Vue({
el: '#colorpicker',
data: {
message: 'hello',
toggle_one: 0,
colors: {
"hex": "#000000",
"source": "hex"
},
colors1: {
"hex": "#ffffff",
"source": "hex"
},
updateValue: '',
hex: '',
isOpen: false,
btnTgl: []
},
components: {
'chrome-picker': Chrome
},
computed: {
style() {
let style = {};
if (this.btnTgl.indexOf(0) > -1) {
style.fontWeight = "bold";
}
if (this.btnTgl.indexOf(1) > -1) {
style.fontStyle = "italic";
}
if (this.btnTgl.indexOf(2) > -1) {
style.textDecoration = "underline";
}
if (this.btnTgl.indexOf(3) > -1) {
style.textDecoration = "line-through";
}
return style;
},
}
});
同样,我只是想在v-bind:样式中加入计算属性而已。谢谢大家的帮助!
答案 0 :(得分:1)
您需要以不同方式绑定样式对象。
<div :style="appliedStyle" class="title">
Random Test Text!!!!!
</div>
Javascript:
var Chrome = window.VueColor.Chrome;
new Vue({
el: '#colorpicker',
data: {
message: 'hello',
toggle_one: 0,
colors: {
"hex": "#000000",
"source": "hex"
},
colors1: {
"hex": "#ffffff",
"source": "hex"
},
updateValue: '',
hex: '',
isOpen: false,
btnTgl: [],
appliedStyle: {}
},
components: {
'chrome-picker': Chrome
},
methods: {
toggle: function() {
this.isOpen = !this.isOpen
this.colors.source = 'hex'
},
style() {
let style = {
'color': this.colors.hex,
'background-color': this.colors1.hex,
}
if (this.btnTgl.indexOf(0) > -1) {
style['font-weight'] = "bold";
}
if (this.btnTgl.indexOf(1) > -1) {
style['font-style'] = "italic";
}
if (this.btnTgl.indexOf(2) > -1) {
style['text-decoration'] = "underline";
}
if (this.btnTgl.indexOf(3) > -1) {
style['text-decoration'] = "line-through";
}
this.appliedStyle = style;
},
},
watch: {
colors: function(val) {
this.appliedStyle['color'] = val.hex;
},
colors1: function(val) {
this.appliedStyle['background-color'] = val.hex;
},
btnTgl: function(val) {
this.style()
}
}
});