5

I'm making a progress bar, which should receive progress status from method submitAction, in which value for progress bar constantly updating. Here my code:

1.Parent component

<template>
    <div>
        <progressbar-component :value="progressState"></progressbar-component>
    </div>
</template>
<script>
    import ProgressBar from './Progress.vue'
    export default {
        components: {
            'progressbar-component': ProgressBar
        },
        data () {
            return {
                ...
                progress: 0
                ...
            }
        },
        computed: {
            ...
            progressState () {
                return this.progress
            }
            ...
        },
        methods: {
            ...
            submitAction: function (event) {
                ...
                let percent = 0
                setInterval(function () {
                    if(someState > 0) {
                        this.progress = percent % 100
                        percent += 10
                    }
                }, 200)
                ...
            }
        }
    }
</script>

2. Child (progress bar) component

<template>
    <div class="progress">
        {{ value }}
    </div>
</template>
<script>
export default {
    name: 'progressbar-component',
    props: {
        value: {
            type: Number,
            default: 0
        }
    }
}
</script>

Aim:

Updating value in Progress Bar's component, while setInterval is running.

Problem:

value doesn't update in child component.

P.S.

Some parts of initial code are just left out to simplify problem representation:

  • this.progress value changes correctly in parent, and I can track it
  • through debugger progress bar component also works correctly and initial value of progress (i.e. 0) passed fine.
10
  • what is some state?? you have if(someState > 0) Commented Apr 6, 2018 at 8:06
  • @roliroli it's array length checking, this part works correctly. Commented Apr 6, 2018 at 8:10
  • ok do you have imported the progressbar component? Commented Apr 6, 2018 at 8:11
  • component works fine, initial data from parent also passed Commented Apr 6, 2018 at 8:15
  • 1
    For the callback function in the setInterval you are using a normal function. Use an arrow function so that this refers to the vue instance and you can acess this.progress Commented Apr 6, 2018 at 8:33

1 Answer 1

3

Well, this took me some time. Classic mistake. The problem is you don't really change components' progress ever:

submitAction: function (event) {        
    let percent = 0
    setInterval(function () {
        if(someState > 0) {
            this.progress = percent % 100    // <---- `this` here doesn't refer to the component
            percent += 10
        }
    }, 200)
}

to make it work do:

submitAction: function (event) {        
    let percent = 0
    setInterval(() => {    // <---- arrow function doesn't have their own `this`, so `this.progress` will refer to the components' value
        if(someState > 0) {
            this.progress = percent % 100 
            percent += 10
        }
    }, 200)
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.