i have a couple of different styling that needs to apply to a text. I am trying to bind the styles using array syntax as shown in the documentation: https://v2.vuejs.org/v2/guide/class-and-style.html but not sure what i’m doing wrong.
I have created a pen for demonstration: https://codepen.io/anon/pen/orVGNP The computed property style is the one i am trying to apply as well which changes the font style and font weight.
JavaScript
x
79
79
1
<div id="colorpicker">
2
<v-layout justify-center>
3
<v-flex class="ml-5">
4
<chrome-picker v-model="colors"></chrome-picker>
5
</v-flex>
6
<v-flex>
7
<chrome-picker v-model="colors1"></chrome-picker>
8
</v-flex>
9
</v-layout>
10
<v-container>
11
<v-layout justify-center>
12
<v-btn-toggle v-model="btnTgl" class="ma-2" multiple>
13
<v-btn>
14
<v-icon>format_bold</v-icon>
15
</v-btn>
16
<v-btn>
17
<v-icon>format_italic</v-icon>
18
</v-btn>
19
<v-btn>
20
<v-icon>format_underlined</v-icon>
21
</v-btn>
22
<v-btn>
23
<v-icon>maximize</v-icon>
24
</v-btn>
25
</v-btn-toggle>
26
<v-flex xs6 class="ml-5">
27
</v-flex>
28
</v-layout>
29
<div v-bind:style="[{ color: colors.hex, background:colors1.hex, style
30
}]" class="title">
31
Random Test Text!!!!!
32
</div>
33
</v-container>
34
</div>
35
36
37
var Chrome = window.VueColor.Chrome;
38
39
new Vue({
40
el: '#colorpicker',
41
data: {
42
message: 'hello',
43
toggle_one: 0,
44
colors: {
45
"hex": "#000000",
46
"source": "hex"
47
},
48
colors1: {
49
"hex": "#ffffff",
50
"source": "hex"
51
},
52
updateValue: '',
53
hex: '',
54
isOpen: false,
55
btnTgl: []
56
},
57
components: {
58
'chrome-picker': Chrome
59
},
60
computed: {
61
style() {
62
let style = {};
63
if (this.btnTgl.indexOf(0) > -1) {
64
style.fontWeight = "bold";
65
}
66
if (this.btnTgl.indexOf(1) > -1) {
67
style.fontStyle = "italic";
68
}
69
if (this.btnTgl.indexOf(2) > -1) {
70
style.textDecoration = "underline";
71
}
72
if (this.btnTgl.indexOf(3) > -1) {
73
style.textDecoration = "line-through";
74
}
75
return style;
76
},
77
}
78
});
79
Again it’s just the computed property that i’m having a hard time trying to include in the v-bind: style. thank you for the help everyone!!
Advertisement
Answer
You need to bind the style object differently.
JavaScript
1
4
1
<div :style="appliedStyle" class="title">
2
Random Test Text!!!!!
3
</div>
4
Javascript:
JavaScript
1
64
64
1
var Chrome = window.VueColor.Chrome;
2
3
new Vue({
4
el: '#colorpicker',
5
data: {
6
message: 'hello',
7
toggle_one: 0,
8
colors: {
9
"hex": "#000000",
10
"source": "hex"
11
},
12
colors1: {
13
"hex": "#ffffff",
14
"source": "hex"
15
},
16
updateValue: '',
17
hex: '',
18
isOpen: false,
19
btnTgl: [],
20
appliedStyle: {}
21
},
22
components: {
23
'chrome-picker': Chrome
24
},
25
methods: {
26
toggle: function() {
27
this.isOpen = !this.isOpen
28
this.colors.source = 'hex'
29
},
30
style() {
31
let style = {
32
'color': this.colors.hex,
33
'background-color': this.colors1.hex,
34
}
35
if (this.btnTgl.indexOf(0) > -1) {
36
style['font-weight'] = "bold";
37
}
38
if (this.btnTgl.indexOf(1) > -1) {
39
style['font-style'] = "italic";
40
}
41
if (this.btnTgl.indexOf(2) > -1) {
42
style['text-decoration'] = "underline";
43
}
44
if (this.btnTgl.indexOf(3) > -1) {
45
style['text-decoration'] = "line-through";
46
}
47
48
this.appliedStyle = style;
49
},
50
},
51
watch: {
52
colors: function(val) {
53
this.appliedStyle['color'] = val.hex;
54
55
},
56
colors1: function(val) {
57
this.appliedStyle['background-color'] = val.hex;
58
},
59
btnTgl: function(val) {
60
this.style()
61
}
62
}
63
});
64