How to pass value from one child component to another in VueJS?

Askar

Full source code: https://github.com/tenzan/menu-ui-tw

Demo: https://flamboyant-euclid-6fcb57.netlify.com/

Goal:

ItemsList and ItemImage are child components to Menu.vue. I need to pass the image_url from ItemsList to ItemImage, in order to change the image on right, after item on left is changed automatically on time intervals.

  • Left side: component ItemsList.vue
  • Right side: component ItemImage.vue

enter image description here

Component Menu.vue has 2 child components:

<template>
  <div>
    <!-- Two columns -->
    <div class="flex mb-4">
      <div class="w-1/2 bg-gray-400">
        <ItemsList />
      </div>
      <div class="w-1/2 bg-gray-500">
        <ItemImage></ItemImage>
      </div>
    </div>
  </div>
</template>

<script>
import ItemsList from "./ItemsList";
import ItemImage from "./ItemImage";

export default {
  components: {
    ItemsList,
    ItemImage
  }
};
</script>

ItemsList.vue:

<template>
  <div>
    <div v-for="item in menuItems" :key="item.name">
      <ul
        class="flex justify-between bg-gray-200"
        :class="item.highlight ? 'highlight' : ''"
      >
        <p class="px-4 py-2 m-2">
          {{ item.name }}
        </p>
        <p class="px-4 py-2 m-2">
          {{ item.price }}
        </p>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuItems: [
        {
          name: "Apple",
          price: 20,
          image_url: "../assets/images/apple.jpg"
        },
        {
          name: "Orange",
          price: 21,
          image_url: "../assets/images/orange.jpg"
        },
        {
          name: "Banana",
          price: 22,
          image_url: "../assets/images/banana.jpg"
        },
        {
          name: "Grape",
          price: 23,
          image_url: "../assets/images/grape.jpg"
        }
      ]
    };
  },
  created() {
    var self = this;
    self.menuItems.map((x, i) => {
      self.$set(self.menuItems[i], "highlight", false);
    });
    var init = 0;
    setInterval(function() {
      if (init === self.menuItems.length) {
        init = 0;
      }
      self.menuItems[init].highlight = true;
      if (init === 0) {
        self.menuItems[self.menuItems.length - 1].highlight = false;
      } else {
        self.menuItems[init - 1].highlight = false;
      }
      init++;
    }, 2000);
  }
};
</script>

<style scoped>
.highlight {
  background-color: gray;
}
</style>

ItemImage.vue - almost empty

<template>
  <div><p>Hello from ItemImage component</p></div>
</template>

<script>
export default {
  props: ["image_url"]
};
</script>

ItemsList iterates through each item and highlights it. I will need component ItemImage to show an image for that active/highlighted item. URL for an image is item.image_url .

Lucifer

Emit an event in ItemsList component with image_url and in Menu component pass image_url to ItemImage component as a prop. I did this in below codesandbox check it out.

https://codesandbox.io/s/wild-moon-mbto4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass data value from one Child Component to another Child Component in React?

In Angular, how to pass value from one component to another when the components are not parent/child?

How to pass callback from one child component to another

VueJs: How to pass a 'computed function' value from child component to the parent component using $emit(optionalPayload) and $on?

How to pass variable value from one component to another in angular 2

How can I pass an *ngIf value from one component to another?

How to pass boolean value from one component to another in react?

How to get the value from one component(Child Component) to another component (Parent Component) in reactjs?

How to pass value from child to parent component

How to pass a value from a child to a parent component?

How to pass parameter from one component to another

How to pass the variable from one component to another

How can I pass value from one component from another component in Angular8 (Independent components)

Pass click event from one child component to another

Angular: Cannot pass output from one child component to another

How to pass data to child component from parent vuejs

VueJS - How to pass Axios response data from parent to child component?

How to pass an API response from a child component to another nested component?

How to pass props of one child component to another child component using parent component

Pass json object one component to another vuejs

How to pass value from a child functional component to parent class component?

How to pass value from parent component to child component (react)

How to send a value from one component to another?

How to send value from one component, to another

how to pass data from one component to another component in angular 2?

How to pass id from one component to another component onclick of an element

How to pass data from one component to another component?

How to pass the data input from one component into another component?

How to pass props from child component to parent component to another child component in ReactJS?