var app = new Vue({
el: '#app',
data() {
return {
myArray: [{ id: 1, name: 'aa' }],
color: "red",
myArray2: []
}
},
computed: {
cssVars() {
return {
"--color": this.color,
};
}
},
methods: {
colorSwtich() {
this.color = this.color == 'red' ? 'green' : 'red'
},
}
});
<div id="app">
<template>
<div :style="cssVars" @click="colorSwtich">
<button class="text">颜色切换-测试文本</button>
</div>
</template>
</div>
.text {
color: var(--color);
}