J'ai ce code de devise api, https://openexchangerates.org/api/currencies.json

Comment remplir json dans la liste déroulante pour VueJs ?

Voici mon code en ce moment:

    new Vue({
        el: "#app",
        data() {
            return {
                currencyFrom: null,
                loading: true,
                errored: false
            };
        },
        mounted() {
            axios
                .get("https://openexchangerates.org/api/currencies.json")
                .then(response => (this.currencyFrom = response))
                .catch(error => {
                    console.log(error);
                    this.errored = true;
                })
                .finally(() => (this.loading = false));
        }
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
  
    <div id="app">
        <div v-if="loading">Loading...</div>
        <select class="form-control" name="currency_from_code" id="currency_from_code" v-else>
         <option v-for="currency in currencyFrom"  >{{ currency }}</option>
        </select>
    </div>

Il parvient à charger le json mais la liste déroulante ne fonctionne pas.

0
Steve 9 nov. 2020 à 05:51

1 réponse

Meilleure réponse

Vous devez définir la réponse.data this.currencyFrom = response.data

Reportez-vous au schéma de réponse axios

new Vue({
        el: "#app",
        data() {
            return {
                currencyFrom: null,
                loading: true,
                errored: false
            };
        },
        mounted() {
            axios
                .get("https://openexchangerates.org/api/currencies.json")
                .then(response => {this.currencyFrom = response.data})
                .catch(error => {
                    console.log(error);
                    this.errored = true;
                })
                .finally(() => (this.loading = false));
        }
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
  
    <div id="app">
        <div v-if="loading">Loading...</div>
        <select class="form-control" name="currency_from_code" id="currency_from_code" v-else>
         <option v-for="currency in currencyFrom"  >{{ currency }}</option>
        </select>
    </div>
1
William Wang 9 nov. 2020 à 02:56