Home > Blockchain >  vue3 It seems that the setup function returns before the onMount function
vue3 It seems that the setup function returns before the onMount function

Time:05-18

createApp({
    setup() {
        let price = 0
    
        onMounted() {
            // axios
            price = axios.response
        }
        return { price }
    }
}).mount('#app')

HTML

<h6 id="app" >{{price}}</h6>

The current situation is that a value of 0 is continuously output. Looking at the order, it seems that the setup function returns before the onMounted function and has already done the rendering.

What should I do?

CodePudding user response:

You should use price = ref(0) for reactivity.

https://vuejs.org/api/composition-api-setup.html#basic-usage

  • Related