Having trouble utilizing Vue3 methods while utilizing the `<script setup lang="ts">` syntax

Looking to incorporate Vue into a non-Vue Class using the Composition API for a Chrome Extension project where having the entire thing as a Vue App doesn't make sense.

The Vue Instance is being instantiated in the usual manner and then injected into the Class.

// typescript
// content.ts
const vueFooInstance = app.mount(vueRoot) as unknown as FooMethodsInterface;
new BarClass(vueFooInstance);

// BarClass.ts
Class BarClass
{
 constructor(vueFooInstance){
  this.vueFooInstance = vueFooInstance
  this.vueFooInstance.someMethodOnFoo();   // encountering an error here if using <script setup> : 
   // this.vueFooInstance.someMethodOnFoo() is not a function
 }
}

The issue seems to be that Vue returns a Proxy Object which results in the method not being available when using the <script setup> approach.

However, switching back to the 'traditional' method resolves the problem:

export default defineComponent({
 setup(){
   someMethodOnFoo(){
   }
  }
  return {
    someMethodOnFoo,
  }
}

Thus, the question arises:

How can the problem be resolved when utilizing the <script setup> method or is it not feasible? (It would also be helpful to understand why the issue occurs with

<script setup lang="ts">
)

(The suspicion is that it might involve a typescript issue with defineExpose, but it's not confirmed).

Answer №1

If you've encountered this issue, it may be because when using <script setup>, the code undergoes a transformation that conceals the inner workings of the component by default. To make specific methods or properties accessible externally, you'll need to utilize defineExpose. In Vue 3 Composition API with <script setup>, employing defineExpose is essential for explicitly exposing methods or properties that you wish to interact with from outside the component. This differs from the options API approach, where you would typically just return an object containing the methods you want to expose.

<script setup lang="ts">
import { defineExpose } from 'vue';

const someMethodOnFoo = () => {
  // Implementation of someMethodOnFoo
};

defineExpose({ someMethodOnFoo });
</script>

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

`Vue testing utilities fail to refresh component following click event`

My current testing setup involves using vue-test-utils/mocha-webpack/expect to test the component code below: <template> <div> <div id="agent-customer-container" class="top-tabs" v-if="agentName || customerName || miniBasket"> ...

Why is the getElement().getProperty("value") function not functioning properly?

I am facing an issue with reading a property in my web component. I am puzzled as to why it is not working correctly. I created a simple example, and after clicking the button, I expect to retrieve the value of the property, but it returns null. I am unsur ...

I am unable to display the service response in the Angular component

I'm facing an issue with displaying data in an angular component from a service. The process from the service to the component seems fine, but when I try to use the variable in HTML, it doesn't show the result. For this project, I am using the M ...

Setting up a beautiful flickity carousel on vuejs and nuxtjs: A step-by-step guide

As a budding vuejs developer, I have been exploring vuejs for some time now and I am ready to dive into a project using this technology. Recently, I came across nuxtjs which offers server-side rendering capabilities. Excitedly, I integrated Bootstrap4 int ...

Unexpected behavior in resolving modules with Babel (using node and typescript)

Within my node project setup, I utilize babel-plugin-module-resolver for managing relative paths efficiently. tsconfig.json { "compilerOptions": { "outDir": "build", "target": "es5", ...

Enhance your Typescript code with a type guard that supports optional wrapped types

I haven't come across a question similar to this one before. My goal is to develop a type guard that can accurately determine the type of a specific variable. It's quite simple with a single type. type A = { id: number, title: string, type: stri ...

Selecting ion-tabs causes the margin-top of scroll-content to be destroyed

Check out the Stackblitz Demo I'm encountering a major issue with the Navigation of Tabs. On my main page (without Tabs), there are simple buttons that pass different navparams to pre-select a specific tab. If you take a look at the demo and click t ...

Issue accessing member value in inherited class constructor in Typescript

My situation involves a class named A, with another class named B that is inherited from it. class A { constructor(){ this.init(); } init(){} } class B extends A { private myMember = {value:1}; constructor(){ super(); ...

Angular routing unit testing: Breaking down routing testing into individual route testing sequences

Currently, I am in the process of testing the routing functionality of my Angular application: Below is the file where I have declared the routes for my app: import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@ ...

Error: monaco has not been declared

My goal is to integrate the Microsoft Monaco editor with Angular 2. The approach I am taking involves checking for the presence of monaco before initializing it and creating an editor using monaco.editor.create(). However, despite loading the editor.main.j ...

Switching the class or modifying the style of an element when it is clicked in Vue.js

The process of retrieving data can be quite intricate. I have an array called "tweets" where the data is stored, and each tweet is represented as a card. I am able to successfully change the style of a card when it's clicked using the markTweet functi ...

The Angular-slickgrid is encountering an issue where it is unable to access the property "hostView" as it

Hey there developers, I've been experimenting with the angular slickgrid library and attempting to incorporate the rowDetailView feature it offers. The issue I'm facing is that while I can expand the view by clicking on it, I'm unable to c ...

How can we ensure file uploads are validated when using class-validator?

Recently, I've been utilizing the wonderful class-validator package to validate data. One specific validation task I'm working on is validating a file upload, ensuring that the file is not empty and ideally confirming that it is an image file. H ...

Prevent updating components when modifying state variables

Introduction I've developed a React component that consists of two nested components. One is a chart (created with react-charts) and the other is a basic input field. Initially, I have set the input field to be hidden, but it becomes visible when the ...

Encountering Problems when Converting Vue Application from JavaScript to TypeScript with Vue and Typescript

Currently, I am in the process of converting a Vue project from JavaScript to TypeScript without utilizing the class-style syntax. These are the steps I took: I ran: vue add typescript I went through all my .vue files and: Indicated that TypeScript ...

React 18 update causes malfunctioning of react-switch-selector component

I'm facing an issue where the component is not rendering. I attempted to start a new project but it still didn't work. Is there a solution to fix this problem or should I just wait for an update from the original repository? Encountered Error: ...

Custom typings for Next-Auth profile

I'm experiencing an issue with TypeScript and Next Auth type definitions. I followed the documentation guidelines to add my custom types to the NextAuth modules, specifically for the Profile interface in the next-auth.d.ts file. It successfully adds t ...

Vue-Loader - Using Vue Loader to Import Components from a File

I am currently in the process of converting Semantic UI CSS framework into Vue Components. I am looking to streamline this process by creating a semantic.js (or possibly semantic.vue) file that will consolidate all component imports, which can then be impo ...

Vue.js error: Trying to access an undefined property

Here is my Vue instance setup: const vueApp = new Vue({ el: '#vue-wrapper', data: { items: [] }}); Utilizing a v-for loop in index.html.eex: <div v-for="item in items[0].subItems">{{ item.name }}</div> In this scri ...

Guide on creating a zodiac validator that specifically handles properties with inferred types of number or undefined

There are some predefined definitions for an API (with types generated using protocol buffers). I prefer not to modify these. One of the types, which we'll refer to as SomeInterfaceOutOfMyControl, includes a property that is a union type of undefined ...