【发布时间】:2020-10-02 23:25:55
【问题描述】:
基于大多数提到单个参数的 vue 文档,
我使用了 v-on:mouseover 并根据每个项目的颜色动态控制样式,因为我需要根据每个项目的颜色通过悬停来更改每个项目的颜色,虽然我使用 !important 和样式,但它不会改变
<li v-for="item in items" v-bind:key="item.id">
<a class="my-link"
v-on:mouseleave="mouseLeave(item)"
v-on:mouseover="mouseOver(item)">
{{ item.title }}
</a>
</li>
data() {
return {
items: [
{
id: 1,
title: "one",
color: "#ccc"
},
{
id: 2,
title: "two",
color: "#000"
},
{
id: 3,
title: "three",
color: "#c7c7c7"
}
]
}
},
methods: {
mouseOver: function(item){
this.$el.children[0].style.color = 'red !important';
},
mouseLeave: function(item){
this.$el.children[0].style.color = `${item.color} !important`;
}
}
【问题讨论】:
-
不要改变 DOM。使用 vue 绑定样式。 vuejs.org/v2/guide/class-and-style.html
-
正如我所说,我尝试了所有单一数据样式,我需要将不同的样式作为颜色传递给 css 或样式
标签: vue.js vue-directives vue-styleguidist