Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to listen all child components rendered in a parent component?

Tags:

synonym.vue

<template>

<div id="synonym-container">
    <div></div>
    <div id="synonym-group-list-wrapper">
        <ul id="synonym-group-list">
            <li v-for="synonymGroup of synonymGroupList" :key="synonymGroup._id">
                <carousel :synonyms="synonymGroup.synonyms"></carousel>
            </li>
        </ul>
    </div>
</div>

</template>

<script>

import Carousel from './synonym-carousel.vue'
import $ from 'jquery'

export default {
    components: {
        'carousel': Carousel,
    },
    mounted() {
        console.log($('.synonym-list'));
    },
}
</script>

synonym-carousel.vue

<template>

<div class="synonym-group">
    <ul class="synonym-list">
        <li></li>
    </ul>
</div>

</template>


<script>

export default {
}
</script>

My goal is that I want to get $('.synonym-list').width() in synonym.vue file so I can change it for all child component in parent component, but I don't know how to manage it.I checked document theres no hook for it. If u have any idea please tell me, thanks!

like image 269
胡亚雄 Avatar asked Jan 10 '17 06:01

胡亚雄


People also ask

How can we access child component data in parents?

We can get child component values in the parent component by creating a reference to the child component using the @ref directive in the Parent component. Using the reference instance, you can access the child component values in the parent.

How do you access child component data in parent component in React?

To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .

How can child components communicate with the parent component?

In the parent component, create a callback function. This callback function will retrieve the data from the child component. Pass the callback function to the child as a props from the parent component. The child component calls the parent callback function using props and passes the data to the parent component.

How do you render a child component within the parent component?

First, you'll need to create two components, one parent and one child. Next, you'll import the child component in the parent component and return it. Then you'll create a function and a button to trigger that function. Also, you'll create a state using the useState Hook to manage the data.


1 Answers

Use events.

<carousel @update="onCarouselUpdate" :synonyms="synonymGroup.synonyms"></carousel>

...

export default {
  components: { ... },
  mounted () { ... },
  methods: {
    onCarouselUpdate () {
      console.log('Carousel Updated!')
    }
  }
}

synonym-carousel:

export default {
  updated () {
    this.$emit('update')
  }
}

docs: https://v2.vuejs.org/v2/guide/components.html#Custom-Events

like image 121
CodinCat Avatar answered Sep 25 '22 10:09

CodinCat