Is there a way to retrieve the ref value from another component file?

Below is the code found in App.vue:

<script setup>
import Nav from "./components/Nav.vue";
</script>

<template>
  <Nav/>
</template>

...................................................................

Now, take a look at the contents of Nav.vue:

<script setup>
import { ref } from "vue";
import plusIcon from "../assets/plusIcon.svg";
import dotsIcon from "../assets/dotsIcon.svg";

import AddCountdownForm from "../components/AddCountdownForm.vue";

const showAddCountdownForm = ref(false);
</script>

<template>
  <div class="relative">
    <nav class="w-full top-0 fixed h-20 bg-gray-200 backdrop-blur-xl mb-14">
      <div
        class="container h-full p-1 flex items-centerm justify-between "
      >
        <!-- add countdown button -->
        <div
          class="my-auto w-14 h-14 p-1 cursor-pointer relative transition-all "
          id="addBtn"
          @click="showAddCountdownForm = true"
        >
          <plusIcon class="fill-indigo-500 h-12 w-12" />
        </div>
        <!-- setting button -->
        <div class="my-auto w-14 h-14 p-1 cursor-pointer relative" id="setting">
          <dotsIcon class="fill-indigo-500 h-12 w-12" />
        </div>
      </div>
    </nav>
    <AddCountdownForm v-show="showAddCountdownForm === true" />
  </div>
</template>

...................................................................

Lastly, let's examine the contents of AddCountdownForm.vue:

<template>
  <div
    class="h-screen w-full bg-gray-200/50 backdrop-blur-sm relative flex md:justify-center md:items-center"
  >
    <div
      class="absolute h-1/2 w-full bg-gray-300 bottom-0 md:bottom-auto md:w-1/2"
    >
      <div class="w-full bg-white h-12 ml-0">
        <div>close</div>
      </div>
      <div>Text</div>
    </div>
  </div>
</template>

If I click on the Plus icon, the form can be shown. However, I am unsure of how to hide it if showAddCountdownForm is located in a separate file.

Answer №1

If you want to trigger an event emission, you can do it like this:

<div @click="$emit('close')">close</div>

Then, to listen for the event being emitted, you can use:

<add-countdown-form v-show="showAddCountdownForm === true" 
  @close="showAddCountdownForm = false" />

const { ref } = Vue
const app = Vue.createApp({})
app.component('navi', {
  template: `
  <div class="relative">
    <nav class="w-full top-0 fixed h-20 bg-gray-200 backdrop-blur-xl mb-14">
      <div class="container h-full p-1 flex items-centerm justify-between ">
        <div class="my-auto w-14 h-14 p-1 cursor-pointer relative transition-all " id="addBtn"
          @click="showAddCountdownForm = true">
        toggle
        </div>
      </div>
    </nav>
    <add-countdown-form v-show="showAddCountdownForm === true" 
      @close="showAddCountdownForm = false" />
  </div>
  `,
  setup() {
    const showAddCountdownForm = ref(false);
    return { showAddCountdownForm }
  }
})
app.component('addCountdownForm', {
  template: `
  <div class="h-screen w-full bg-gray-200/50 backdrop-blur-sm relative flex md:justify-center md:items-center">
    <div class="absolute h-1/2 w-full bg-gray-300 bottom-0 md:bottom-auto md:w-1/2">
      <div class="w-full bg-white h-12 ml-0">
        <div @click="$emit('close')">close</div>
      </div>
      <div>Text</div>
    </div>
  </div>
  `,
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" integrity="sha512-wnea99uKIC3TJF7v4eKk4Y+lMz2Mklv18+r4na2Gn1abDRPPOeef95xTzdwGD9e6zXJBteMIhZ1+68QC5byJZw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div id="demo">
  <navi></navi>
</div>

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

Join and Navigate in Angular 2

Attempting to retrieve information from a JSON file has been an issue for me. Here is the code snippet: ngOnInit() { this.http.get('assets/json/buildings.json', { responseType: 'text'}) .map(response => response) .subsc ...

The function or attribute "registerUser" is not specified on the instance, yet it is being referenced during the rendering process

index.blade.php <div id="register"> ... <div> <button type="button" class="btn btn-primary" @click="registerUser">Add</button> </div> </div> manage.js var DomainUsers = { template: `...`, d ...

How to target a class in jQuery that contains two specific strings

There are multiple images in my HTML, each assigned two classes. Here's a snippet of the relevant code: class = "thing other0-a" class = "thing other1-a" class = "thing other2-a" class = "thing other3-a" class = ...

Struggling with handling various active states in ReactJS?

Good day to all my fellow stackOverflowers! I'm looking for advice on how to maintain the independence of active states when clicked. I'm struggling to prevent them from affecting each other. Each button starts with an active status of false by d ...

The type 'Observable<Response>' does not include a property called 'map'

I recently started learning angular and npm, but encountered an error while trying to replicate some code from a source I found (linked here). It seems like the error is related to my development environment, but I'm having trouble pinpointing the ex ...

Experiencing the issue of receiving unexpected commas following a div

Here is the code written in TypeScript for creating an HTML table that displays items from nested objects. The code is functional, but there seems to be an issue with extra commas being printed which are not part of any specific line being executed. im ...

Using jQuery, identify when any of the elements within every function are missing

In my JavaScript file, I have the following code snippet: var aryYears= []; $(".year").each(function(){ aryYears.push($(this).val()); }) This allows me to pass an array of years as a parameter in the saveChanges function. I want to make ...

Is there an appropriate method in Vue/Nuxt for managing and altering component data without using lifecycle hooks?

Scenario: I am dealing with a component that showcases a datatable listing Applications. Upon clicking a row, it triggers the opening of another component with appropriately loaded fields (for new or edit). The Challenge: The component loads a reference t ...

Diving into Redux brings up the question of whether to use a generic reducer or a

When working with Redux, what is the preferred approach: Creating entity-specific reducers or utilizing a generic reducer based on strict action keying conventions? The former can result in more boilerplate code but offers loose coupling and greater flexib ...

Error: Unable to find the transport method in Socket.io

I recently implemented user side error logging on my website to track errors. I have noticed that sometimes it logs a specific error related to socket.io code: TypeError: this.transport is undefined This error seems to only occur for users using Firefox ...

Expanding and collapsing all divs with a single click using Jquery

I've gone through numerous resources but I'm still struggling to understand this concept. I have implemented a simple accordion based on this tutorial and now I want to include an "expand/collapse all" link. While I was able to expand all the ite ...

Modifying the onclick function for a bootstrap glyphicon

How can I swap the .glyphicon-menu-down to glyphicon-menu-up when a user clicks on a link? Currently, it's not working as expected. Could there be an issue with my jQuery implementation? Markup <a data-toggle="collapse" data-parent="#accordion" h ...

Modify the divs in two separate occasions when submitting different forms

Within my form, I have radio buttons located inside div1, a captcha code to be solved in div2, and additional content in div3. My goal is to have div1 replaced by div2 when a radio button is checked and submitted. Then, after completing the captcha in div2 ...

Troubleshooting the issue: JavaScript's Time comparison failure with JSON versus Local time

Dealing with JSON data like the following: [ { "hourly_AQI": 73.0, "hourly_date": "Tue, 31 Oct 2023 11:00:00 GMT" }, { "hourly_AQI": 79.0, "hourly_date": "Tu ...

Guide to showcasing object characteristics inside an object in Angular2

My USAFacts object contains properties like StateName, as well as objects like State-Bird which hold information about the state bird. If written in JSON, a record of USAFacts would appear as follows: {"StateName": "PA", "State-Bird": [ { "Name": "Ruffed ...

How can I efficiently store and access DOM elements in my React application to avoid constant re-fetching?

Is there a known pattern for storing references to both regular DOM elements and jQuery elements within a class? Despite the general advice against using jQuery with React, I needed the jQuery functions for my menu to function properly and did not have ti ...

Sharing JSON data with public/javascripts/X.js in Node/Express: A beginner's guide

Currently facing a challenge with my Express project. Take a look at https://github.com/MoreeZ/help1 and examine ./public/javascripts/budget.js. My goal is to swap the incomeData object to read data from ./incomedata.json I attempted passing it through th ...

What is the correct way to integrate the Ant Design library with Next.js for seamless server-side rendering?

I duplicated the official Next.js example using Ant Design at this link: https://github.com/vercel/next.js/tree/canary/examples/with-ant-design After cloning, I proceeded with npm install to install all dependencies. Then, I ran npm run dev to check if ev ...

Creating a shimmering glow for a dynamic AJAX div block in real-time

I created an Ajax code that retrieves results from a txt file in real time, which are then automatically displayed in a div block using the following lines: if (xmlhttp.responseText != "") { InnerHTMLText = xmlhttp.responseText + document.getElementBy ...

Access the Vue component's data in Laravel blade template

Seeking an easy way to retrieve a value from a component in Laravel blade, <input name="avatar" type="hidden" :value="$refs.webcam.img" /> <webcam-avatar ref="webcam"></webcam-avatar> Although I am ...