Clicking on a search result does not trigger the popup to open in the App.vue file

Whenever I click on a search item, my goal is to open the Popup. Why does this.$emit('openPopup', bookId); not work in the selectBook(bookId) method?

In Results.vue component, the search results are displayed using Google Books API:

<template>
  <div class="results">
    <ul class="results-items">
      <li
        class="book"
        @click="selectBook(result.id)"
      >
        <img
          :src="'http://books.google.com/books/content?id=' + result.id + '&printsec=frontcover&img=1&zoom=1&source=gbs_api'"
          class="cover"
        >
        <div class="item-info">
          <div class="bTitle">{{ result.volumeInfo.title }}</div>
          <div
            class="bAutors"
            v-if="result.volumeInfo.authors"
          >
            {{ result.volumeInfo.authors[0] }}
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  props: ['result'],
  data() {
    return {
      bookId: '',
    };
  },
  methods: {
    selectBook(bookId) {
      this.$router.push(`bookId${bookId}`);
      this.$store.dispatch('selectBook', bookId);
      this.$emit('hideElements', bookId);
      this.$emit('openPopup', bookId);
    },
  },
};
</script>

Results screenshot

Here is the main App.vue where I intend to display the Popup:

<template>
  <div id="app">
    <div class="container">
      <Header />
      <popup
        v-if="isOpenPopup"
        @closePopup="closeInfoPopup"
        @openPopup="showModal"
      />
      <router-view/>
    </div>
  </div>
</template>

<script>
import Header from '@/components/Header.vue';
import Popup from '@/components/Popup.vue';
import 'bootstrap/dist/css/bootstrap.css';

export default {
  components: {
    Header,
    Popup,
  },
  data() {
    return {
      isOpenPopup: false,
    };
  },
  methods: {
    showModal() {
      console.log('click');
      this.isOpenPopup = true;
    },
    closeInfoPopup() {
      this.isOpenPopup = false;
    },
  },
};
</script>

The Popup.vue component:

<template>
  <div class="popup">
    <div class="popup_header">
      <span>Popup name</span>
    </div>
    <div class="popup_content">
        <h1>Hi</h1>
      <slot></slot>
      <button @click="closePopup">Close</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'popup',
  props: {},
  data() {
    return {};
  },
  methods: {
    closePopup() {
      this.$emit('closePopup');
    },
  },
};
</script>

Answer №1

Remember, when working with a popup component, make sure to trigger events on the Result component itself. Events are bound to the component they belong to.

If you find yourself in this situation, here are a few options to consider:

  1. Add event listeners (such as @openPopup) only on the Result component and utilize the portal-vue library to render the popup at the top level.
  2. Utilize global events by using this.$root.emit and listening on this.$root. Ensure to add event listeners in the mount() hook and remember to unregister them in the beforeDestroy() hook.
  3. Consider using Vuex to manage popup visibility and related data in a global application store provided by Vuex.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it necessary to include specific versions in package.json when committing the yarn.lock file?

Do you believe it is beneficial to always commit the yarn.lock file, even though all versions are clearly specified in package.json and there should be no discrepancies among team members? I personally find it to be a time-consuming practice, especially w ...

Ways to incorporate JavaScript code within Reactjs

I am currently working with Reactjs and using Nextjs. I am facing a challenge regarding integrating "index.html" with "index.js". At the bottom of "index.html", there is some JavaScript code that I need to transfer to another file. Can you advise me on w ...

Exploring the contrast between RFC and RFCE in the realm of ReactJS

As a newcomer to Reactjs, I'm curious about the distinction between "react functional component" and "react functional component export". Can someone help explain? Below is an example of a react functional component: import React from 'react&apo ...

Tips for creating dynamic row styles in a fluid table layout?

Could someone help me figure this out? I'm not familiar with JavaScript and need assistance as I've never created it, only edited existing scripts. I have a layout with two tables side-by-side. On mobile devices, the second table is pushed below ...

Is it possible to verify with Jest that ReactDOM.render has been called if it's enclosed in a conditional statement?

I have a React component that conditionally calls ReactDOM.render when root === true to satisfy flow: import React from 'react' import ReactDOM from 'react-dom' import App from './App' const root = document.getElementById(& ...

What is the best way to showcase an image using Next.js?

I'm having a problem with displaying an image in my main component. The alt attribute is showing up, but the actual image is not appearing on the browser. Can someone please point out what I might be doing wrong? import port from "../public/port. ...

`Populating fields in Firebase``

I am currently in the process of transitioning from a Mongoose + Express app to using Firebase + Express. However, I am facing some challenges with populating related fields similar to how it is done in Mongoose. Is there a more efficient way to achieve th ...

Utilizing jQuery for array selection

If there's an array of HTML elements with the following structure... let info = [<tr>...</tr>, <tr class="name">...</tr>, <tr class="colour">...</tr>] How can one select only the class name from the info array usi ...

Utilizing cloud functions to distort an inappropriate image

I have a requirement to analyze any uploaded image for inappropriate content and blur it if necessary. The log this image is inappropriate indicates that the detection process is working correctly. However, I am not able to see any further logs which sugg ...

What steps are involved in creating a deployment version of a Vue.js CLI website?

After setting up my Vue.js CLI project, I successfully ran npm run dev and accessed it in the browser at port 8080. Then, I proceeded to make some modifications to the website. Next, I executed npm run build. I then transferred the contents of the dist fol ...

When attempting to connect to the MongoDB cloud, an unexpected error arises that was not present in previous attempts

npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="650800170b4816001713001725544b554b55">[email protected]</a> start > nodemon index.js [nodemon] 3.0.2 [nodemon] to restart at any time, enter ...

Exploring ways in JavaScript to locate every occurrence of a string within segments of URLs embedded in HTML code

I have a large HTML file (~50MB) and I need to extract all instances of strings that are between two specific strings (which contain forward slashes) in URLs. var content = '<div><a href="https://sample.org/something/here/091209283/?para ...

The jQuery carousel script is not functioning

Currently developing a jQuery slideshow for my website, and I stumbled upon a code that fits my requirements perfectly. I made some adjustments to the code from this source: . My specifications were that the code should be created from scratch, lightweight ...

Filtering out specific properties from an element within a JavaScript forEach loop

const findBloodType = bloodCodeArray.find(bloodCode => bloodCode.code.toUpperCase() === bloodType.toUpperCase()).code; In my Angular code, I am trying to retrieve just the 'code' property of the 'bloodCode' element using a callback ...

Tips for correctly deleting a duplicate ref Object ID following the removal of a Document

Coming from a background in relational databases, I'm encountering a challenge with a pattern in Mongoose. Let's say we have SchemaA and SchemaB (for example, pets and people): const Person = new Schema({ name: String, pets: [{ ref: ...

Utilize the Power of React.js and Express.js to Send Emails

After building a web app using React.js in ES6, I found myself wanting to add a "Contact Us" page that allows users to send an email. However, as a newcomer to React, I discovered that React itself cannot directly send emails. Following tutorials with node ...

Is there a way to speed up the loading time of React in the browser?

My experience with using React in the browser has been positive, but I'm facing a speed issue. The initial loading time is quite lengthy as it compiles. I am looking for a way to use React/JSX directly in the browser and have it compile instantly wit ...

Encountered an error while building Vue 2 with docker - sh: vue-cli-service is missing

I am facing an issue while trying to create a Vue 2 app with Docker. The error message sh: vue-cli-service: not found is appearing during the build process. It's strange because everything works as expected in the serve command, but when building, thi ...

Incorporate h:outputText into your JavaScript code segment

After populating some information into a h:outputText, I am now looking to trigger a JavaScript function upon clicking a button that will retrieve the text from the outputText. The structure of the code is as follows: <p:tabView id="tabView"> & ...

The functionality of Express JS routers is experiencing issues

As I delved into learning nodejs and express, I decided to create a basic router. Initially, everything seemed to be working fine, but upon reopening the project, I encountered an issue. var express = require('express'); var app = express(); var ...