【问题标题】:how to use setInterval in vue component如何在 vue 组件中使用 setInterval
【发布时间】:2017-09-06 05:22:21
【问题描述】:

我在每个my-progress中定义了定时器,用来更新view的值,但是控制台显示的值是常量变化的,而view的值还是没有变化,怎么在定时器中改变view的值

Vue.component('my-progress', {
    template: '\
            <div class="progress progress-bar-vertical" data-toggle="tooltip" data-placement="top">\
                <div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" :style="{height: pgvalue}">{{pgvalue}}\
                </div>\
            </div>\
        ',
    data : function(){  

        return {
            pgvalue : '50%',
            intervalid1:'',
        }
    },
    computed:{

        changes : {
            get : function(){
                return this.pgvalue;
            },
            set : function(v){
                this.pgvalue =  v;
            }
        }
    },
    mounted : function(){

        this.todo()     
    },
    beforeDestroy () {

       clearInterval(this.intervalid1)
    },
    methods : {

        todo : function(){          
            this.intervalid1 = setInterval(function(){
                this.changes = ((Math.random() * 100).toFixed(2))+'%';
                console.log (this.changes);
            }, 3000);
        }
    },
})

这里是链接: jsbin.com/safolom

【问题讨论】:

    标签: javascript vuejs2 vue-component


    【解决方案1】:

    this 没有指向 Vue。试试

    todo: function(){           
        this.intervalid1 = setInterval(function(){
            this.changes = ((Math.random() * 100).toFixed(2))+'%';
            console.log (this.changes);
        }.bind(this), 3000);
    }
    

    todo: function(){  
        const self = this;          
        this.intervalid1 = setInterval(function(){
            self.changes = ((Math.random() * 100).toFixed(2))+'%';
            console.log (this.changes);
        }, 3000);
    }
    

    todo: function(){  
        this.intervalid1 = setInterval(() => {
            this.changes = ((Math.random() * 100).toFixed(2))+'%';
            console.log (this.changes);
        }, 3000);
    }
    

    How to access the correct this inside a callback?

    【讨论】:

      【解决方案2】:

      检查这个例子:

      Vue.component('my-progress-bar',{
      	template:
      `<div class="progress">
      	<div
      		class="progress-bar"
      		role="progressbar"
      		:style="'width: ' + percent+'%;'"
      		:aria-valuenow="percent"
      		aria-valuemin="0"
      		aria-valuemax="100">
      		{{ percent }}%
      	</div>
      </div>`,
      	props: { percent: {default: 0} }
      });
      
      
      new Vue({
      	el: '#app',
      	data: {p: 50},
      	created: function() {
      		var self = this;
      		setInterval(function() {
              if (self.p<100) {
                   self.p++;
               }
          }, 100);
      	}
      });
      <script src="https://cdn.jsdelivr.net/npm/vue"></script>
      <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
      
      <div id='app'>
        <my-progress-bar :percent.sync='p'>
        </my-progress-bar>
        <hr>
        <button @click='p=0' class='btn btn-danger bt-lg btn-block'>
        Reset Bar Progress
        </button>
      </div>

      【讨论】:

        猜你喜欢
        • 2020-07-02
        • 2019-04-19
        • 1970-01-01
        • 2021-10-20
        • 2021-02-12
        • 2018-04-17
        • 2021-09-21
        • 2019-04-17
        • 2020-12-17
        相关资源
        最近更新 更多