[Vuejs]-Re-populate a directive array reactively in Vue.js

0👍

This solution should work and maintain reactivity.

  1. You should be able to replace the entire array without using splice or set.

  2. I have used a closure to capture this because sometimes the fetch call interferes with the this reference, even inside a lambda expression.

countryChangeHandler() {
        this.states = []
        const that = this
        fetch(`/scripts/statejson.php?country=${this.country}`)
            .then(response => response.json())
            .then(res => {
                that.states = res.states.map(it=>it.urlid)              
            })
    },

Leave a comment