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

280
Visualizações
Vue 3 not updating inline style

I set up some demo code for vue 3, for updating inline style to a computed object, but it doesn't update.

<!DOCTYPE html>
<html>
    <head>
        <title>Demo</title>
        <link rel="stylesheet" href="main.css">
        <script src="https://cdn.jsdelivr.net/npm/vue@3.2.26/dist/vue.global.prod.min.js"></script>
    </head>

    <body>
        <div id="map">
            <div id="infantry" class="unit" :style="style">
                Position: {{ x }} {{ y }}
            </div>  
        </div>
    
        <script>
            const Infantry = {
                data() {
                    return {
                        x: 0,
                        y: 0
                    }
                },
                mounted() {
                    setInterval(() => {
                        this.x++;
                        this.y++;
                    }, 1000);
                },
                computed : {
                    style() {
                        return {
                            top : this.x
                        };
                    }
                }
            }

            Vue.createApp(Infantry).mount('#infantry');
        </script>
    </body>
</html> 

This part doesn't work

:style="style"

I check the dom, and it doesn't set the style to use top. Anyone know what's wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is that you're binding an attribute of the element where you mount the vue app then add px to the returned top value:

.unit {
  position: absolute
}
<!DOCTYPE html>
<html>

<head>
  <title>Demo</title>
  <link rel="stylesheet" href="main.css">
  <script src="https://cdn.jsdelivr.net/npm/vue@3.2.26/dist/vue.global.prod.min.js"></script>
</head>

<body>
  <div id="map">
    <div class="unit" :style="style">
      Position: {{ x }} {{ y }}
    </div>
  </div>

  <script>
    const Infantry = {
      data() {
        return {
          x: 0,
          y: 0
        }
      },
      mounted() {
        setInterval(() => {
          this.x++;
          this.y++;
        }, 1000);
      },
      computed: {
        style() {
          return {
            top: this.x + "px"
          };
        }
      }
    }

    Vue.createApp(Infantry).mount('#map');
  </script>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
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