Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue reload child component

Tags:

vue.js

vuejs2

I'm using vue, version 2.5.8

I want to reload child component's, or reload parent and then force children components to reload.

I was trying to use this.$forceUpdate() but this is not working.

Do You have any idea how to do this?

like image 549
Piotr Żak Avatar asked Dec 06 '17 21:12

Piotr Żak


People also ask

How do I refresh my Vue component?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

How do I remount a component Vue?

To remount a component in Vue. js, we should change the key prop of the component. to set the key prop of my-component to componentKey . And then we add a button to toggle the componentKey between true and false .

How do I destroy Vue components?

To make a component delete itself in Vue. js, we can use the this. $destroy method. to add the close method to call this.

How do you call a Vue phone mount?

The `mounted()` Hook in Vue Vue calls the mounted() hook when your component is added to the DOM. It is most often used to send an HTTP request to fetch data that the component will then render. For example, the below Vue component uses the mounted() hook to make an HTTP request to the JSONPlaceholder API.


4 Answers

Use a :key for the component and reset the key.

See https://michaelnthiessen.com/force-re-render/

like image 140
sureshvv Avatar answered Sep 28 '22 14:09

sureshvv


Add key to child component, then update the key in parent. Child component will be re-created.

<childComponent :key="childKey"/>
like image 20
Bagaskara Avatar answered Sep 28 '22 13:09

Bagaskara


If the children are dynamically created by a v-for or something, you could clear the array and re-assign it, and the children would all be re-created.

To simply have existing components respond to a signal, you want to pass an event bus as a prop, then emit an event to which they will respond. The normal direction of events is up, but it is sometimes appropriate to have them go down.

new Vue({
  el: '#app',
  data: {
    bus: new Vue()
  },
  components: {
    child: {
      template: '#child-template',
      props: ['bus'],
      data() {
        return {
          value: 0
        };
      },
      methods: {
        increment() {
          this.value += 1;
        },
        reset() {
          this.value = 0;
        }
      },
      created() {
        this.bus.$on('reset', this.reset);
      }
    }
  }
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.min.js"></script>
<div id="app">
  <child :bus="bus">
  </child>
  <child :bus="bus">
  </child>
  <child :bus="bus">
  </child>
  <button @click="() => bus.$emit('reset')">Reset</button>
</div>

<template id="child-template">
  <div>
  {{value}} <button @click="increment">More!</button>
  </div>
</template>
like image 30
Roy J Avatar answered Sep 28 '22 14:09

Roy J


I'm using directive v-if which is responsible for conditional rendering. It only affects reloading HTML <template> part. Sections created(), computed are not reloaded. As I understand after framework load components reloading it is not possible. We can only re render a <template>.

Rerender example.

I have a Child.vue component code:

<template>
    <div v-if="show">
      Child content to render 
      {{ callDuringReRender() }}
    </div>
</template>

<script>
    export default {
        data() {            
            return {               
                show: true
            }
        }
        ,
        methods: {
            showComponent() {
                this.show = true
            },
            hideComponent() {
                this.show = false
            },
            callDuringReRender() {
                console.log("function recall during rendering")
            }
        }
}
</script>

In my Parent.vue component I can call child methods and using it's v-if to force the child rerender

<template>
    <div>
      <input type="button" 
       value="ReRender the child" 
       @click="reRenderChildComponent"/>

      <child ref="childComponent"></child>

    </div>
</template>

<script>
    import Child from './Child.vue'

    export default {
       methods: {
            reRenderChildComponent(){
                this.$refs.childComponent.hideComponent();
                this.$refs.childComponent.showComponent()
            }
       },
       components: {
            Child
       }
    }
</script>

After clicking a button in console You will notice message "function recall during rendering" informing You that component was rerendered.

like image 36
Piotr Żak Avatar answered Sep 28 '22 15:09

Piotr Żak