Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
Vue3 computed property changing its value inside onMounted hook

I have just been using and learning Vue for a week, and developing a small vue3 application using a CDN because that's the requirement. I'm implementing using a composition api. I have a computed property which is shown below.

Note that state.allCvls is a reactive variable which is a result of an axios.get call from a rest api.

...
const allCvlCount = computed (() => {
    return state.allCvls.length; // This value is generated from an axios call from a restapi.
});
...

onMounted (() => {
    console.log(allCvlCount);
    console.log(allCvlCount.value);
})

When I try to print the object and its value property, they have different values as shown in the image below.

enter image description here

I tried to create a test case and its not behaving the same way as above. Where can I start debugging to pin down the cause of this discrepancy.

Here is my test case which is working fine.

<!DOCTYPE html>
<html>
<head>
<style>
#app {
  height: 400px;
}
</style>
</head>
<body>
<div id="app">
  <!-- <v-chart autoresize :option="myOptions"/> -->
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.prod.js"></script>
<script>
    const { createApp, reactive, computed, onMounted } = Vue
    const app = createApp({
        setup() {
            const state = {
                myReactiveVar: [{'name': 'foo', 'count': 5}, {'name': 'bar', 'count': 3}, {'name': 'xyz', 'count': 8}]
            }
            const myComputedProperty = computed(() => {
                return state.myReactiveVar.filter(e => e.count <= 5).length;
            });

            onMounted(() => {
                console.log(myComputedProperty);
                console.log(myComputedProperty.value);
            });
            
            return {
                state,
                myComputedProperty
            };
        }
    })
    .mount("#app");
</script>
</body>
</html>

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda