【问题标题】:Vue toggle color and text of button when clicked单击时Vue切换按钮的颜色和文本
【发布时间】:2020-01-05 19:03:00
【问题描述】:

我有一个按钮,我已经成功地修改过,所以点击它会调用一个函数

<button class="btn btn-primary btn-block" v-on:click="pauseEvent" type="button" role="button" id="" aria-expanded="false" style=" color: #6c757d; background-color: #FEEFB3; border:none; border-radius: .15;">
                Pause Event
            </button>

问题是我无法获得用于更改颜色和文本的功能。我已将其缩小到至少控制台在单击时记录一个触发器并且它正在工作。我应该如何正确地做到这一点,以便单击将在当前状态和另一个颜色为红色且文本为“恢复事件”的状态之间切换?

pauseEvent() {
      console.dir('paused');
    },

【问题讨论】:

  • 这是关于 a) 切换一个状态 b) 基于状态渲染颜色和文本。尝试分别解决每个问题。

标签: javascript vue.js


【解决方案1】:
<button class="btn btn-primary btn-block" :style="{ 'background-color': color }" v-on:click="pauseEvent" type="button" role="button" id="" aria-expanded="false" style=" color: #6c757d border:none; border-radius: .15;">
            {{text}}
        </button>

您可以使用 Vue 数据功能。 Example of here

        data(){
            return {
                color: 'green',
                text: 'Started'
            }
        },
        methods: {
           pauseEvent() {
               this.color = 'red';
               this.text = 'Paused';
           },
        }

【讨论】:

  • 其实:它切换到暂停/红色,但它没有切换回来?
  • 为此,您可以打开Toggle功能并在数据中保留当前状态。如果函数中的状态是暂停,可以相应的改变颜色。
猜你喜欢
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-14
  • 2021-01-20
  • 2021-04-27
  • 1970-01-01
相关资源
最近更新 更多