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

190
Visualizações
Create a reactive Proxy with VueJs

I'm working on a little project where I implemented a simple versioning system.
I have an array of objects (the diferent versions) that I wrap in a Proxy which will give me the informations of the desired version.

I use this object in a VueJs component and would like the view to be reactive when I change the version property of the Proxy.
How could I manage that ?

So far, to do so, I forgot about the proxy idea and just used a computed property. But I'd like to keep my Proxy for aesthetic reason.

Here is a simplified version of what I'd like

function versioning (versionArray) {
  return new Proxy(versionArray, {
    version: 0,
    latest: versionArray.length - 1,
    get: function (target, prop) {
      if (["version", "latest"].includes(prop)) {
        return this[prop];
      } else {
        return target[this.version][prop];
      }
    },
    set: function (target, prop, val) {
      if (prop === "version") {
        return this.version = val;
      } else {
        return false;
      }
    }
  })
}

let myData = versioning([
  { 
    title: "My title",
    description: "My first desption" 
  },
    { 
    title: "THE title",
    description: "My first description" 
  },
  { 
    title: "THE title",
    description: "It's a nice description" 
  }
]);
myData.version = myData.latest;

Vue.createApp({
  data() {
    return {
      myData,
    }
  },
  methods: {
    version(val) {
      this.myData.version = Math.min(Math.max(this.myData.version+val, 0), this.myData.latest);
    }
  }
}).mount('#app');
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.31/vue.global.prod.min.js"></script>
<div id="app">
  <button @click="version(1)">
    &lt;&lt;
  </button>
  <div>{{myData.title}}</div>
  <div>{{myData.description}}</div>
  <button @click="version(-1)">
    &gt;&gt;
  </button>
</div>

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