mardi 26 février 2019

Update select options - [Vue warn]: Computed property "options" was assigned to but it > has no setter

I'm writing my first Laravel Vue component and I have this error in console.

[Vue warn]: Computed property "options" was assigned to but it has no setter.

I just miss to update the options of the second select with the updated values I have in this.countries.

After the change this.countries get updated but the values of the options of the country_id select are not changing. I've tried adding computed but I get this error.

What am I doing wrong?

<template>
    <div>
        <div class="form-group continent_id">    
            <select name="continent_id" v-model="continent_selected" id="continent_id" class="selectpicker" data-live-search="false" title="Pick a continent" v-on:change="getAllCountries(continents)">
                <option v-if="continents.length>0" v-for="continent in continents" v-bind:value="continent.id">
                    
                </option>
            </select>
        </div>

        <div class="form-group country_id">    
            <select name="country_id" v-model="country_selected" id="country_id" class="selectpicker" data-live-search="true" title="Pick a country">
                <option  v-for="(country, index) in countries" v-bind:value="country.id" >
                    
                </option>
            </select>
        </div>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.');
            this.loadData();
            console.log('Loaded datas.');
            console.log(this.continents);
            console.log(this.countries);
        },
        created(){
            //this.loadData();
        },
        data() {
            return {
                continents: [],
                countries: [],
                continent_selected: '',
                country_selected: '',
            }
       },
       computed: {
            options: function(event) {
               return this.countries
            }
        },

       methods: {
            loadData: function() {
                axios.get('/api/continents')
                .then((response) => {
                    // handle success
                    this.continents = response.data.data;
                    this.getAllCountries(this.continents);
                })
                .catch((error) => {
                    // handle error
                    console.log(error);
                })
                .then(() => {
                    // always executed
                });
            },
            getAllCountries: function(continents) {
                console.log(this.continent_selected);
                console.log(continents);

                var j = 0;
                this.countries = [];

                for (var i = 0, len = continents.length; i < len; i++) {

                    if (!this.continent_selected){
                        for (var key in continents[i].active_countries) {
                            this.countries[j] = {id: continents[i].active_countries[key], name: key};
                            j++;
                        }
                    }
                    else{
                        console.log("continent selected: "+ this.continent_selected);
                        for (var key in continents[i].active_countries) {
                            if (continents[i].id == this.continent_selected){
                                this.countries[j] = {id: continents[i].active_countries[key], name: key};
                                j++;
                            }
                        }
                        this.options = this.options;
                    }
                }
            }
        },
    }
</script>



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2UahkTR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire