를 사용하는 방법 setInterval 에서 구성 요소 vue

정의에서 타이머 각 나의 진행,사용하는 값을 업데이트의 콘솔을 보여줍니다 상수의 값을 변경의 가치 보기 여전히 변경되지 않는,어떻게 할 수 있습니에서 타이머 값을 변경하 보기

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

해결책

가리키지 않을 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);
}

시에 액세스하는 방법이 올바른내부의 콜백?]1

해설 (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
해설 (0)