Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vue 3, where to listen for emit event

I’m sending an event from a child component to my parent component using $emit.
I know I should tell the parent component to listen to that event, however I don’t know where to put the

@openModal="changeModalVisibility"

Should I do it as an attribute in the main div? Since this is the highest component I don’t find the right place.

Child:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<template>
  <div class="cocktail-result" v-on:click="openModal">
    <img :src="cocktailImg" alt="cocktail-image" />
    <p>{{ cocktailName }}</p>
    <div class="explore-button">
      <img
        src="../assets/img/arrow-forward-icon.svg"
        alt="arrow icon"
        class="arrow-icon"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "CocktailResult",
  props: {
    cocktailName: {
      type: String,
    },
    cocktailImg: {
      type: String,
    },
    modalClass: {
      type: String,
    },
  },
  methods: {
    openModal() {
      this.$emit("openModal");
    },
  },
};
</script>

Parent:

<template>
  <div class="main-container" v-on:openModal="changeModalVisibility">
    <div class="search-bar-container">
      <img src="./assets/img/logo.png" alt="logo" class="logo" />
      <div class="search-bar">
        <input
          type="text"
          v-model="searchQuery"
          class="search-bar-input"
          placeholder="Enter Your Favorite Cocktail:"
          @input="callCocktailApi"
        />
        <img
          src="./assets/img/close-icon.svg"
          alt="close icon"
          class="close-icon"
          v-on:click="clearInput"
        />
      </div>
    </div>
    <div class="result-container">
      <ul v-for="(cocktail, index) in cocktailArray" :key="cocktail.key">
        <CocktailResult
          :cocktailName="this.cocktailArray[index].strDrink"
          :cocktailImg="this.cocktailArray[index].strDrinkThumb"
          :cocktailGlass="this.cocktailArray[index].strGlass"
          :cocktailInstructions="this.cocktailArray[index].strInstructions"
          :modalClass="this.modalClass"
          :method="changeModalVisibility"
        />
      </ul>
    </div>
    <CocktailModal :modalClass="this.modalClass" />
  </div>
</template>

<script>
import CocktailResult from "./components/CocktailResult.vue";
import CocktailModal from "./components/CocktailModal.vue";
import axios from "axios";

export default {
  name: "App",
  data() {
    return {
      cocktailArray: "",
      searchQuery: "",
      cocktailName: "",
      cocktailImg: "",
      modalClass: "hidden",
    };
  },
  components: {
    CocktailResult,
    CocktailModal,
  },
  methods: {
    callCocktailApi() {
      axios
        .get(
          `https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${this.searchQuery}
    ` // API Call
        )
        .then((res) => {
          this.cocktailArray = JSON.parse(
            JSON.stringify(res.data.drinks || [])
          );

          this.cocktailName = this.cocktailArray[0].strDrink;

          // Only render results if an array is fetched

          console.log(this.cocktailArray);
        })
        .catch((error) => {
          console.log(error);
        });
    },

    clearInput() {
      this.cocktailArray = "";
      this.searchQuery = "";
    },

    changeModalVisibility() {
      this.modalClass = "";
      console.log("I'm working");
    },
  },
};
</script>

>Solution :

You should put @openModal in your parent component, there were you call CocktailResult.

<CocktailResult 
 @openModal=“YourCallback”
 … 
/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading