Tips for resolving the issue of 'defineExpose' method being undefined in Vue3

Struggling to pass a method from child to parent; unfortunately, the defineExpose() method seems to be malfunctioning. Could anyone provide guidance on what might be going wrong?

For additional context, feel free to check out my previous question

<script setup lang="ts">
import { useHead } from '@vueuse/head'

import { pageTitle } from '/@src/state/sidebarLayoutState'

pageTitle.value = 'Saved Topical'
useHead({
  title: 'Saved Topical',
})

defineExpose({
  
})
</script>
{
  "name": "vuero",
  "version": "1.2.1",
  "private": "true",
  "license": "MIT",
  ...
}

https://i.sstatic.net/eoEav.png

Your help is greatly appreciated!

Answer №1

Encountered a similar issue while working with Vue 3. Resolved it by updating my TypeScript version from 4.5.5 to 4.9.3, which seemed to work like magic. It seems that older versions of TypeScript do not support compiler macros such as defineExpose.

To silence the ESLint errors, I also had to add a prefix (as mentioned by @stefan):

// eslint-disable-next-line no-undef
defineExpose({  });

Answer №2

When working in VS Code, it is recommended to disable Veture for your project's workspace and opt for using Volar instead.

Answer №3

Important Update!

In order for your Vue code to work correctly, make sure you are using version 3.1.3 or higher as this is when the `defineExpose` feature was introduced.

For more information, check out the changelog at here.


I'm not entirely certain what's causing the issue in your code where you're trying to add `defineExpose`. Here's a modified version of your code (I adjusted some variables due to my local setup):

Parent.vue

<script setup lang="ts">
import { ref } from 'vue';
import { Notyf } from 'notyf';
import Child from './Child.vue';

const addNewPaper = ref();
const notyf = new Notyf();

const successSave = () => {
  addNewPaper.value.savePaper();
  notyf.success('Your paper has been successfully created!');
};
</script>
<template #content>
  <input type="button" @click="successSave" value="Save" />
  <Child ref="addNewPaper"></Child>
</template>

Child.vue

<script setup lang="ts">
import { Notyf } from 'notyf';
import { computed, defineProps, reactive, ref, defineExpose } from 'vue';

const companySize = ref('');
const businessType = ref('');
const productToDemo = ref('');
const date = ref(new Date());

const initialState = reactive({
  subject: '',
  paper: '',
  marks: '',
});

const notyf = new Notyf();

const props = defineProps({
  subjects: { required: true },
});

const savePaper = () => {
  notyf.success('Paper saved successfully');
};

defineExpose({
  savePaper,
});
</script>
{
  "name": "vuero",
  "version": "1.2.1",
  "private": "true",
  "license": "MIT",
// Additional dependencies and devDependencies truncated for brevity
}

Answer №4

It looks like there is an issue with eslint here. Just before defineExpose, you could include the following comment:

//eslint-disable-next-line no-undef
defineExpose({  })

Alternatively, you can simply click on the quick Fix button that appears when you hover over defineExpose.

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

A guide on demonstrating time using the AngularJS date filter

Is it possible to display the time in AM and PM using the angular date filter? I attempted to achieve this with the provided code, however, it did not yield the desired result. <div>{{ '12:31:07' | date:'HH:mm' }}</div> ...

Utilizing React-Router-Redux alongside the powerful features of React-Bootstrap

I've been struggling with this issue for some time now! My goal is to create a 'main app container' that consistently displays the logo, navigation... I plan on using react-bootstrap to enhance its appearance. Currently, I'm encounter ...

Issue with linear Graham scan method causing failure when computing convex hull of basic polygon

It is said that the Graham scan algorithm can efficiently find the convex hull of a simple polygon in linear time without requiring the nlogn sorting step since the vertices are already effectively sorted. I have implemented the Graham scan algorithm, and ...

React Router's nested route causes a full page reload when navigating

I have been working on setting up nested routing in React Router and here is my code: import React from 'react'; import DefaultSwitch from './components/DefaultSwitch/DefaultSwitch'; import './scss/App.scss'; const App = () ...

How can I trigger an Iframe JavaScript function from within my webpage?

I have an Iframe within my page, with the following JavaScript code: function getTotSeats(){ window.WebAppInterface.showToast(document.forms[0].txtSeat_no.value); return document.forms[0].txtSeat_no.value; } I would like to call the above Jav ...

Is time-based revalidation in NextJS factored into Vercel's build execution time?

Currently overseeing the staging environment of a substantial project comprising over 50 dynamic pages. These pages undergo time-based revalidation every 5 minutes on Vercel's complimentary tier. In addition, I am tasked with importing data for numer ...

VSCode is unable to locate the typeRoots type declarations

I have organized all the type definitions that will be used in my application into a single file. I created a folder named @types and an index.d.ts file that exports every interface/type needed. I updated my tsconfig.json to include the @types folder: { ...

When using React Ant Design, the form.resetFields() function does not trigger the onChange event of the Form.Items component

In my project, I am working with the Ant Design <Form> component and handling onChange events within <Form.Items>. Whenever the onChange event function evaluates to true, additional content is displayed dynamically. For instance, in the code s ...

Encountered an error of 'No overload matches this call' while configuring ContextApi alongside useReducer with Typescript

I am currently setting up ContextApi and useReducer with typescript. I am encountering an error with my "issuesInitialState" variable in useReducer, where I receive the following message: No overload matches this call. Overload 1 of 5, '(reducer: ({ ...

Troubleshooting a problem with selecting options in Jquery

Looking for assistance with a jquery script. I have a select dropdown that fetches a list of options via Ajax. When a user clicks on an option, if a certain variable equals 1, an HTML div is added. If the variable changes to another value, the HTML div dis ...

Learn how to utilize ajax and jquery to fetch data from an external database without the need for refreshing the page

I'm facing an issue with a small script. I want to query the Content in the database on a simple HTML page without any page refresh. <form method="get" name="formrrv" id="formrrv"> <input type="hidden" name="calculate" value="1 ...

Creating a Stroke on an html5 canvas with the help of EaselJS

As a newcomer to Easel and HTML5, I am attempting to draw a line on a canvas using EaselJS. The X-coordinate is set to 100 while the Y-coordinate is taken from an array list. Below is the code I have written. Can someone please help me identify where my ...

Is there a way to maintain scroll position on a dynamically refreshing div?

After searching for hours, I still can't find a solution to my problem. I am using a simple JQuery script to refresh a div and display my PHP output in it. $script = " <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery ...

Obtain a string value from a JavaScript object

My dilemma involves a specific Javascript object. { A: 1, B: 2, C: 2, D: 1, E: 1, F: 4, G: 6, H: 2 }, The goal is to extract a four-letter string based on the key with the highest value, but there are limitations. The stri ...

When an input event is dispatched in a unit test, the value changes of a form are not activated

Currently, I am testing a scenario where I need to verify if a value changes on the form when input is typed in. This particular project utilizes Nrwl nx as well as jest for testing purposes. The component code snippet is as follows: export class InputNu ...

Google App Engine does not properly interpret PHP code when making AJAX requests

I am currently facing an issue with using AJAX request on Google App Engine. In my local development environment, everything works fine and the request is correctly interpreted. However, when I deploy the code to production, the AJAX request renders the co ...

An error has occurred in Typescript stating that there is no overload that matches the call for AnyStyledComponent since the update to nextjs

Upgraded to the latest version of nextjs, 13.3.1, and encountered an error in the IDE related to a styled component: Received error TS2769 after the upgrade: No overload matches this call. Overload 1 of 2, '(component: AnyStyledComponent): ThemedSty ...

Error: Unable to perform 'getComputedStyle' on the 'Window' object: the first parameter must be of type 'Element'

My goal is to create a rotating effect with a center circle containing 5 inner divs. When the device is rotated on the gamma axis, I want the circle div to rotate in accordance with the gamma degree, while the inner divs rotate in the opposite direction to ...

Step-by-step guide on implementing Form.create in TypeScript and Ant Design with function components

Having trouble compiling my form created using Form.create(). The error message I'm getting is: Argument of type 'FunctionComponent<MyProps>' is not assignable to parameter of type 'ComponentType<{}>'. Type 'Fu ...

Trouble with Callback firing in Select2's 'Infinite Scroll with Remote Data' feature

After reviewing the tutorial on the Select2 project page, I am implementing a feature to load additional records as the user scrolls to the end of the results. <script> $(document).ready(function() { $('#style_full_name').select2({ ...