简体   繁体   中英

VueJS data not reactive mounted

I have an API which returns all the currency rate, i used a function getRate() on mounted but rate['usd'] is undefined , if i call the function again on that page it returns the actual data, i tried beforeCreated beforeMounted but they are not working, how to make the data reactive on load or am i doing something wrong?

<template>
  <span v-text="rate['usd']"></span>
</template>

<script>
data() {
    return {
        rate: null
    }
},
methods: {
    getRate() {
        this.$vs.loading()
        this.$http.post('wallet/rate' ,[])
        .then(response => {
            for(let key in response.data.data.data){
                this.rate[response.data.data.data[key].name] = response.data.data.data[key].value
            }
            this.$vs.loading.close()
        })
        .catch(error => {
            this.$vs.loading.close()
        })
    },
},
mounted() {
  this.getRate()
}
</script>

Does this work?

<template>
  <span v-text="rate.usd"></span>
</template>

<script>
data() {
    return {
        rate: null
    }
},
methods: {
    getRate() {
        const rate = {}
        this.$vs.loading()
        this.$http.post('wallet/rate' ,[])
        .then(response => {
            for(let key in response.data.data.data){
                rate[response.data.data.data[key].name] = response.data.data.data[key].value
            }
            this.$vs.loading.close()
            this.rate = rate
        })
        .catch(error => {
            this.$vs.loading.close()
        })
    },
},
mounted() {
  this.getRage()
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM