¿Puedes forzar el SDV?js para recargar/volver a renderizar?


Solo una pregunta rápida.

Puedes forzar el SDV.js para recargar/recalcular todo? Si es así, ¿cómo?

Author: Dave, 2015-08-20

9 answers

Prueba este hechizo mágico:

vm.$forceUpdate();

No hay necesidad de crear vars colgantes:)

Actualización: Encontré esta solución cuando solo empecé a trabajar con VueJS. Sin embargo, la exploración posterior demostró este enfoque como una muleta. Por lo que recuerdo, en un tiempo me deshice de él simplemente poniendo todas las propiedades que no se actualizaron automáticamente (en su mayoría anidadas) en propiedades calculadas.

Más información aquí: https://vuejs.org/v2/guide/computed.html

 111
Author: Boris Mossounov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-10 14:33:25

Esto parece una solución bastante limpia de matthiasg en este problema :

También puede usar :key="someVariableUnderYourControl" y cambiar la clave cuando desee que el componente se reconstruya completamente

Para mi caso de uso, estaba alimentando un Vuex getter en un componente como prop. De alguna manera Vuex recuperaría los datos, pero la reactividad no se activaría de manera confiable para volver a procesar el componente. En mi caso, establecer el componente key a algún atributo en la prop garantizó una actualización cuando los getters (y el atributo) finalmente se resuelven.

 19
Author: Brian Kung,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-12 20:44:22

Use vm.$set('varName', value). Busque los detalles en Change_Detection_Caveats.

 16
Author: denis.peplin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-03 18:41:22

¿Por qué?

...¿ necesidad para forzar una actualización?

Tal vez usted no está explorando el SDV en su mejor momento:

Tener SDV automáticamente react to value changes, the objects must be initially declared in data. O, si no, deben añadirse usando Vue.set().

Vea los comentarios en la demostración a continuación. O abra el la misma demo en un JSFiddle aquí.

new Vue({
  el: '#app',
  data: {
    person: {
      name: 'Edson'
    }
  },
  methods: {
    changeName() {
      // because name is declared in data, whenever it
      // changes, Vue automatically updates
      this.person.name = 'Arantes';
    },
    changeNickname() {
      // because nickname is NOT declared in data, when it
      // changes, Vue will NOT automatically update
      this.person.nickname = 'Pele';
      // although if anything else updates, this change will be seen
    },
    changeNicknameProperly() {
      // when some property is NOT INITIALLY declared in data, the correct way
      // to add it is using Vue.set or this.$set
      Vue.set(this.person, 'address', '123th avenue.');
      
      // subsequent changes can be done directly now and it will auto update
      this.person.address = '345th avenue.';
    }
  }
})
/* CSS just for the demo, it is not necessary at all! */
span:nth-of-type(1),button:nth-of-type(1) { color: blue; }
span:nth-of-type(2),button:nth-of-type(2) { color: red; }
span:nth-of-type(3),button:nth-of-type(3) { color: green; }
span { font-family: monospace }
<script src="https://unpkg.com/vue"></script>

<div id="app">
  <span>person.name: {{ person.name }}</span><br>
  <span>person.nickname: {{ person.nickname }}</span><br>
  <span>person.address: {{ person.address }}</span><br>
  <br>
  <button @click="changeName">this.person.name = 'Arantes'; (will auto update because `name` was in `data`)</button><br>
  <button @click="changeNickname">this.person.nickname = 'Pele'; (will NOT auto update because `nickname` was not in `data`)</button><br>
  <button @click="changeNicknameProperly">Vue.set(this.person, 'address', '99th st.'); (WILL auto update even though `address` was not in `data`)</button>
  <br>
  <br>
  For more info, read the comments in the code. Or check the docs on <b>Reactivity</b> (link below).
</div>

Para dominar esta parte de Vue, verifique los documentos Oficiales sobre Reactividad - Advertencias de Detección de Cambios. Es una lectura obligada!

 11
Author: acdcjunior,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-28 14:30:02

Intente usar this.$router.go(0); para recargar manualmente la página actual.

 4
Author: PoyChang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-08 09:06:55

Encontré una manera. Es un poco raro, pero funciona.

vm.$set("x",0);
vm.$delete("x");

Donde vm es su objeto de modelo de vista, y x es una variable inexistente.

SDV.js se quejará de esto en el registro de la consola, pero activa una actualización para todos los datos. Probado con la versión 1.0.26.

 2
Author: Laszlo the Wiz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-03 22:11:27

Puede forzar el re-renderizado de la interfaz de usuario: este.forc forceUpdate ();

 2
Author: Panayiotis Hiripis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-29 09:33:19

Usando la directiva v-if

<div v-if="trulyvalue">
    <component-here />
 </div>

Así que simplemente cambiando el valor de trulyvalue de falso a verdadero causará que el componente entre el div vuelva a enviar

 1
Author: GEOFFREY MWANGI,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-08 20:56:15

Claro .. simplemente puede usar el atributo key para forzar el re-renderizado (recreación) en cualquier momento.

<mycomponent :key="somevalueunderyourcontrol"></mycomponent>

Véase https://jsfiddle.net/mgoetzke/epqy1xgf / para un ejemplo

También se discutió aquí: https://github.com/vuejs/Discussion/issues/356#issuecomment-336060875

 0
Author: mgoetzke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-18 14:34:06