更改 class 动态无法正常工作

Changing class dynamic doesn't work properly

我正在学习 Vue.js,目前我正在学习与 css 交互,我真的很想知道为什么我的代码不起作用,基本上我有一个按钮,当我单击该按钮时,每隔 1 秒(我为此使用了 setInterval,我测试了 setInterval 并且它有效)它应该在我在 css 上定义的 2 classes 之间变化,我有一个高亮 class 和收缩 class,它应该在 1 秒内相互交换,当我输入示例时,第二个 class 已附加但经过 1 秒后没有变化发生,你们能解释一下为什么吗?

Html相关部分

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <script src="https://unpkg.com/vue@2.2.1"></script>

  <div id="exercise">
    <!-- 1) Start the Effect with the Button. The Effect should alternate the "highlight" or "shrink" class on each new setInterval tick (attach respective class to the div with id "effect" below) -->
    <div>
      <button @click="startEffect">Start Effect</button>
      <div id="effect" :class="{highlight: effect,shrink: !effect}"></div>
    </div>

Css

#effect {
  width: 100px;
  height: 100px;
  border: 1px solid black;
}

.highlight {
  background-color: red;
  width: 200px !important;
}

.shrink {
  background-color: gray;
  width: 50px !important;
}

Javascript

  new Vue({
  el: '#exercise',
  data: {
    effect: true,
  },
  methods: {
    startEffect: function() {
      setInterval(function(){
        this.effect = !this.effect;
        console.log(this.effect);
      },1000);
    }
  }
});

您在 setInterval 中丢失了 component 的上下文,因此您的 this 不是您期望的 vue-component

new Vue({
    el: '#exercise',
    data: {
      effect: true,
    },
    methods: {
      startEffect: function() {
        console.log(this) // add this line
        setInterval(function() {
          this.effect = !this.effect;
          console.log(this) // with this line, it will show you what went wrong
          console.log(this.effect);
        },1000);
      }
    }
})

您可以:

new Vue({
  el: '#exercise',
  data: {
    effect: true,
  },
  methods: {
    startEffect: function() {
      setInterval(() => {
        this.effect = !this.effect;
        console.log(this.effect);
      },1000);
    }
  }
})

这是可行的,因为 arrow functions this 绑定到封闭范围。

(ES5)

new Vue({
  el: '#exercise',
  data: {
    effect: true,
  },
  methods: {
    startEffect: function() {
      var vm = this
      setInterval(function() {
        vm.effect = !vm.effect;
        console.log(this.effect);
      },1000);
    }
  }
})

这是因为您不再在 setInterval's 回调中使用 this,而是使用变量 vm 来保存组件的上下文。


看看效果如何

new Vue({
  el: '#exercise',
  data: {
    effect: true,
  },
  methods: {
    startEffect: function() {
      setInterval(() => {
        this.effect = !this.effect;
        console.log(this.effect);
      },1000);
    }
  }
})
#effect {
  width: 100px;
  height: 100px;
  border: 1px solid black;
}

.highlight {
  background-color: red;
  width: 200px !important;
}

.shrink {
  background-color: gray;
  width: 50px !important;
}
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="exercise">
    <!-- 1) Start the Effect with the Button. The Effect should alternate the "highlight" or "shrink" class on each new setInterval tick (attach respective class to the div with id "effect" below) -->
    <div>
      <button @click="startEffect">Start Effect</button>
      <div id="effect" :class="{highlight: effect,shrink: !effect}">     </div>
</div>