0

I have this currency code api, https://openexchangerates.org/api/currencies.json

How to populate json into dropdownlist for VueJs?

This is my code at the 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>

It does manage to load the json but the dropdownlist is not working.

1 Answer 1

1

You should set the response.data this.currencyFrom = response.data

Refer to axios response schema

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.