可以使用计算属性或方法将动态样式应用于元素

发布于 2025-01-25 15:26:31 字数 672 浏览 5 评论 0原文

试图在动态样式中使用传播操作员(在这个简化的示例中不用有用,但在我的真实情况中使用多种样式对象很重要)。此外,我需要使用带有参数的方法,但它不起作用。实际上,即使有一个计算的属性,它也无效。

我在做什么错?

https://jsfiddle.net/goldorak/79wrj301/2/

<div id="app">
  <div class="item" :style="{ ...styleComputed }">
    item
  </div>
  
  <div class="item" :style="{ ...styleMethod('red') }">
    item
  </div>
</div>

new Vue({
  el: "#app",
  computed: {
        styleComputed: function(){
        return { 'background-color': 'red' };
    }
  },
  methods: {
    styleMethod: function(val){
        return { 'background-color': val };
    }
  }
})

Trying to use spread operator in dynamic styles (not useful in this simplified example but important in my real case with multiple style objects). In addition I need to use a method with a parameter, but it doesn't work. In fact even with a computed property it doesn't work.

What am I doing wrong?

JSFiddle

Markup:

<div id="app">
  <div class="item" :style="{ ...styleComputed }">
    item
  </div>
  
  <div class="item" :style="{ ...styleMethod('red') }">
    item
  </div>
</div>

Script:

new Vue({
  el: "#app",
  computed: {
        styleComputed: function(){
        return { 'background-color': 'red' };
    }
  },
  methods: {
    styleMethod: function(val){
        return { 'background-color': val };
    }
  }
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

混吃等死 2025-02-01 15:26:31

就像您可以从摘要中看到的那样,您只是忘了参考Vue:

new Vue({
  el: "#app",
  computed: {
    styleComputed() {
      return { 'background-color': 'red' };
    }
  },
  methods: {
    styleMethod(val) {
      return { 'background-color': val };
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
<div id="app">
  <div class="item" :style="{ ...styleComputed }">
    item
  </div>
  
  <div class="item" :style="{ ...styleMethod('blue') }">
    item2
  </div>
</div>

Like you can see from snippet you just forgot to reference vue:

new Vue({
  el: "#app",
  computed: {
    styleComputed() {
      return { 'background-color': 'red' };
    }
  },
  methods: {
    styleMethod(val) {
      return { 'background-color': val };
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
<div id="app">
  <div class="item" :style="{ ...styleComputed }">
    item
  </div>
  
  <div class="item" :style="{ ...styleMethod('blue') }">
    item2
  </div>
</div>

一袭水袖舞倾城 2025-02-01 15:26:31

我猜破坏性打破了反应性吗?

您应该使用数组绑定,而是将多个对象绑定到类 /样式中:

<div
  :style="[styledComputed, { opacity: 1 }]"
  :class="['static-class', { active: isActive }, myComputedClasses]"
>
</div>

I guess that the destructuring breaks the reactivity?

You should use the array binding instead to bind multiple objects into class / style:

<div
  :style="[styledComputed, { opacity: 1 }]"
  :class="['static-class', { active: isActive }, myComputedClasses]"
>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文