0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue の忘れがちな事のメモ

Last updated at Posted at 2020-01-01

値の変更をトリガーにしてタイマーセットする

値の変更を検知してタイマーセットする。
v-on などのイベントによらず、値変更がトリガーになるので適用範囲が広がる。

clearInterval を使ってリセットしないと、次の実行から interval 0 になる。

var t; // timer
var timerRun; // timer status
var timerSet = new Vue({
  el: '#timer_obj',
  data: {
    message: '',
    icon: ''
  },
  methods: {
    icon_clear: function(){
      this.icon = '';
      this.message = '';
      timerRun = false; // done
    }
  },
  watch: {
    // triggered when changed
    icon: function(val){
      if (t && ! timerRun) {
        clearInterval(t) ; // reset interval 
      }
      if (val !== '') {
        timerRun = true;
        t = setInterval(this.icon_clear,3000); // method
      }
    }
  }
});

コンポーネント間でやりとりする

  • Props Down, Event Up
  • emit を使う

cf.

computed はキャッシュする

Vue のエラー防止ルール

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?