Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

260
Vistas
Is there a proper way to mutate a Vue prop from an external Javascript?

I'm working on a site made using PHP templates and jQuery, with a table rendered using Vue:

let vuetablecomponent= new Vue({
  components: { ManualTable },
}).$mount('[data-vue-app=manualtable]');

I need to be able to manipulate the state of the Vue table from the outside. Currently I'm manipulating the props directly using JS, like this:

vuetablecomponent.$children[0].aprop = 'propvalue'

And of course, I got a warning from Vue:

Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value.

Technically this shouldn't be a problem, since the parent component is not a Vue component, and I do want the value to be overwritten if the page is re-rendered by PHP. But I'm wondering if there's a more... proper way to do this.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

How do you render the component and pass the prop to the component on first render?

Whether you use PHP templates or plain HTML, you still have to initialize the Vue app somehow because otherwise how would the browser know how to load a Vue component?

You either did:

Method A (use render method):

--- init-app.js --
new Vue({
  el: '#app',
  render: h => h(App)
})


-- App.vue --
<template>
  <MyTableComponent :a-prop="tableProp" />
</template>

<script>
export default {
  data() {
    return {
      tableProp: "123-abc",
    };
  },
}
</script>

If above is the case, just create a reference to this component like so:

mounted() {
  window.__APP__ = this
}

then update the tableProp wherever you want by doing window.__APP__.tableProp = "new-value"

Method B (use html content as template):

--- index.php ---
<?php
<html>
  <body>
    <div id="app">
      <MyTableComponent :a-prop="tableProp"></MyTableComponent>
    </div>
  </body>
</html>
?>
new Vue({
  el: "#app",
  component: {
    MyTableComponent,
  },
  data: {
    tableProp: "123-abc",
  }
})

If this is the case, just create a reference to the app like so:

window.__APP__ = new Vue({})

make sure Vue gets loaded first before your script that updates this prop so the variable is actually set.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda