Ways to retrieve information and functions from defineExpose in Vue

`I am attempting to call a method from a child component within the parent component in Vue using the composition API. I have defined the method inside defineExpose, but I am unable to access it in the parent component.

Hero.vue (ChildComponent)

<template>
  <section data-testid="hero-section" class="flex flex-row gap-2 justify-between ">
    <h1 class="text-xl font-semibold text-ds-dark-500">
      {{ greeting }}
    </h1>
    <div>
      <Input v-model="searchQueryValue" data-testid="hero-section-input" placeholder="Search across use case" licon="fas fa-search" liconClasses="text-dark-60% px-1" inputClasses="rounded-full !border-1 !border-dark-20%" @input="handleSearch" />
    </div>
  </section>
</template>

<script setup lang="ts">
import { computed, defineEmits, ref } from 'vue';
import useUserDerivedDetails from '~/composables/useUserDerivedDetails';
import Input from '~/components/Input.vue';
import { useTranslation } from 'i18next-vue';
import { getGreeting } from '../utils';

const emit = defineEmits(['search', 'clearSearch']);
const { t: $t } = useTranslation();

const { user } = useUserDerivedDetails();

const searchQueryValue = ref('');
const userName = computed(() => {
  const name = user.value.firstName || user.value.lastName || '';
  return `${name}`.trim();
});

const greeting = computed(() => {
  return $t(getGreeting(searchQueryValue.value, userName.value));
});

function handleSearch(e: Event) {
  emit('search', e);
}
function clearSearch() {
  searchQueryValue.value = '';
  emit('clearSearch');
}

defineExpose({
  clearSearch,
});
</script>

I attempted to employ ref and assign it to the component, however, it did not work as expected`

Answer №1

Implement a ref:

const $mainCharacter = ref()

//executed post-loading

$mainCharacter.value.startGame()

// when used in template

<character ref=”$mainCharacter" .. 

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 there a way to disable Google fonts and Material Design icons in Vuetify?

I'm working on an internal app for our company that operates offline, so I can't utilize CDNs or Google Fonts. When checking my developer console in Chrome (oddly not in Firefox), I see errors indicating the use of the following URLs: and In N ...

Guide to removing a Firebase collection within a Vue.js component

I'm working on a Vue.js component that retrieves data dynamically from Firebase, and I want to make sure I unsubscribe when exiting the component. The Firebase documentation specifies using the unsubscribe() function to stop listening to the collecti ...

Tips for informing flowtype of expanding a partial options object to ensure it is fully developed by a specific stage

Most of you are probably familiar with a very simple use case from your projects. Imagine you have a utility class or function that looks like this: type Options = { foo?: string }; class Something { static get defaultOptions(): Options { ...

Using the 'useMediaQuery' function from Material UI in a class component

Currently experimenting with Material UI's useMediaQuery feature to adjust the styles of a specific component, in this case the Button component. The objective is to eliminate the outline surrounding the button when the screen size decreases. The atte ...

Tips for seamlessly transitioning the background of Google Maps into view as you scroll down the page

I have a unique background with a Google Map. It loads perfectly in the right place when I open the page, but as I scroll down, it disappears from view. Is there a way to keep the Google map constantly in the background, even when scrolling? This is my cu ...

Unable to display the content

Why isn't the text expanding when I click "see more"? Thanks XHTML: <div id="wrap"> <h1>Show/Hide Content</h1> <p> This code snippet demonstrates how to create a show/hide container with links, div eleme ...

What is the reason behind having a selectedOptions property rather than just a selectedOption?

Why does the selectedOptions property of select return an HTMLCollection instead of just one HTMLOptionElement? As far as I understand (correct me if I'm wrong), a select element can only have one selected option, so why do I always need to access it ...

Experiencing difficulties when trying to pan and zoom the data obtained from d3.json within a line chart

I am currently working on developing a trend component that can zoom and pan into data fetched using d3.json. Here is the code I have written so far: <script> var margin = { top: 20, right: 80, bottom: 20, left: 50 }, width = $("#trendc ...

Simply touch this element on Android to activate

Currently utilizing the following code: <a href="http://www...." onmouseover="this.click()">Link</a> It is evident that this code does not work with the Android Browser. What javascript code will function properly with the Android Browser as ...

Is it possible for a popup to appear without any user interaction

Do you ever wonder how certain websites are able to trigger pop-ups without being blocked by Chrome's pop-up blocker? I had always thought that pop-up blockers only allowed window.open if it was initiated by a user action. However, the situation seem ...

Transmit the data.json file to a node.js server using Postman

Hi there, I have a file named data.json saved on my desktop that I want to send to a node.js function. The contents of my data.json file are structured as follows: [{"key":"value"}, {same key value structure for 8000 entries}] This fil ...

Import reactjs modules without the need for Browserify, Webpack, or Babel

I am attempting to set up a TypeScript HTML application in Visual Studio. My goal is to incorporate reactjs v0.14.7 without relying on tools like Browserify. But, how can I utilize the react-dom module in this scenario? Let's set aside TypeScript fo ...

Step-by-step guide on implementing a fade effect to a specific JavaScript element ID

When I click a button, the image on the screen disappears. I am looking to add a fade effect to this action. Can someone help me achieve this using CSS? #hide{-webkit-transition: all 1s ease-in-out;} For reference, here is a demo showcasing the current b ...

Dynamically validate AngularJS forms with JSON Schema

I am currently trying to dynamically validate JSON in AngularJS. Unfortunately, I have encountered an issue with loading fields from the schema onto the page. My understanding of AngularJS is limited as I am still new to it. Although I have managed to cr ...

Unable to reach props during mounted lifecycle stage

I am currently working on a Vue component that looks like this: <template> <div> <div class="page-head"> <h4 class="mt-2 mb-2"> {{form.name}} </h4> </div> <f ...

Utilizing Material-UI components for a seamless navigation button experience

After reviewing the example on https://material-ui.com/guides/composition/#button, I have implemented a button in my main page: <BrowserRouter> <Box m={2}> <Button size="large" variant="cont ...

Generating consecutive numerical values within the auto table JSPDF column VusJs

columns: [ { header: 'No', dataKey: 'Index', }, { header: 'No Registrasi', dataKey: 'no_register' }, { header: 'Kode Partai', dataKey: 'kode_partai' }, ...

Executing "mounted" in VueJS SSR with Quasar: A step-by-step guide

In accordance with the documentation: Given that there are no dynamic updates, only beforeCreate and created Vue lifecycle hooks will be triggered during SSR. This implies that any code within other lifecycle hooks like beforeMount or mounted will sole ...

The module '@algolia/cache-common' is missing and cannot be located

summary: code works locally but not in lambda. My AWS lambda function runs perfectly when tested locally, utilizing Algolia within a service in the server. Despite installing @algolia/cache-common, any call to the lambda results in a crash due to the erro ...

How can I disable the 'Leave site?' dialog box for Google Forms embedded in an iframe?

Within my Vue component, I have integrated Google Forms using an iframe. However, a challenge arises when users attempt to navigate to another page and are prompted with the 'Leave site? Changes that you made may not be saved' dialog. Is there a ...