Access prop through watchers in VueJS

1

A similar problem has been solved in this issue:

Access prop in method with VueJS

Where prop was null since the ajax request of the parent component was not yet completed; but in my case the prop is undefined even after the component is completely set up and the data path via ajax is completed.

export default {
    props: ['orders'],
    filterID: null,
...
watch: {
    filterID: (value) => {
        console.log(this.orders) // undefined
    }
}
    
asked by anonymous 16.01.2018 / 19:28

1 answer

1

Give default value to this orders (I assume it is of type array ) and so when the value changes the Vue, by reactivity it will update the code that it depends on it.

So instead of

props: ['orders'],

usa

props: {
    orders: {
        type: Array,
        default: () => ([])
    }
}

And so you can already use filters , computed or in the template directly that it will be updated.

    
16.01.2018 / 20:23