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

Tips for Setting up SQLite Database Connection in VueNative App

Struggling to establish a connection to SQLite database in vueNative. Unsure of the steps to take. Could someone lend a hand? Seeking guidance on how to connect to SQLite db within a vueNative app. ...

Create an overlay effect when hovering in a Vue.js application

I am currently facing a challenge in implementing text display on image hover in vue.js. I have tried to replicate this functionality using an array with multiple images following this example: here. Although my vue file is quite extensive, the crucial pa ...

Typescript: create a type similar to keyof but with a particular value type

I have an interface called MyInterface interface MyInterface { field1: boolean, field2: MyType, field3: MyType } In this interface, I want to create a new type that contains only the keys which have values of type MyType. While I know about the key ...

Toggle a Vue.js method to display responses for a particular question

Currently, I am in the process of developing a simple toggle feature for a FAQ section. The idea is that when a user clicks on an icon associated with a specific question, only that question's answer should be displayed. Although the function is oper ...

Issue with Typescript and eslint errors occurring exclusively in fresh code - Anticipated a colon.ts(1005)

Lately, in my extensive react-typescript project, I have encountered a peculiar issue. It seems that syntax errors are popping up everywhere, but only within the new code that I write. For instance, when creating a new component: import React from 're ...

Executing Functions from Imported Modules in Typescript

Is there a way to dynamically call a method from my imported functions without hard-coding each function name in a switch statement? I'm looking for a solution like the following code: import * as mathFn from './formula/math'; export functi ...

Validator returns undefined when expressing invalid data

Having an issue with validation, here is the code snippet: routes.js var express = require('express'); var router = express.Router(); var hello_controller = require('../api/controllers/helloController'); var { validationRules, validat ...

Securing Email and Password Data in Cypress Tests

Greetings! I trust everyone is in good spirits. My dilemma lies in the fact that I am hesitant to include email and passwords in version control. I am considering using environment variables in my cypress tests and utilizing secrets for runtime value pro ...

Issue with vueform/multiselect component when used as a single file component

I've been encountering some unusual errors while attempting to integrate vueform/multiselect. Most of the examples I've come across don't make use of single-file components, but that's the method I prefer. My goal is to import the Multi ...

Exploring the various form types supported by 'react-hook-form'

I utilized react hooks form to create this form: import React from "react"; import ReactDOM from "react-dom"; import { useForm, SubmitHandler } from "react-hook-form"; import "./styles.css"; function App() { type ...

What is the correct way to set the default function parameter as `v => v` in JavaScript?

function customFunction<T, NT extends Record<string, string | number | boolean>>( data: T, normalize?: (data: T) => NT, ) { const normalizedData = normalize ? normalize(data) : {}; return Object.keys(normalizedData); } customFuncti ...

Custom options titled MUI Palette - The property 'primary' is not found in the 'TypeBackground' type

I am currently working on expanding the MUI palette to include my own custom properties. Here is the code I have been using: declare module '@mui/material/styles' { interface Palette { border: Palette['primary'] background: Pa ...

Looking for guidance on where to find a functional code sample or comprehensive tutorial on working with ViewMetadata in Angular2

I am currently trying to understand the relationship between viewmetadata and the fundamental use of encapsulation: ViewEncapsulation, including ViewEncapsulation.Emulated and ViewEncapsulation.None. Here is a link for further information: https://angula ...

No errors encountered during compilation for undefined interface object types

Hey there, I'm currently exploring the Vue composition API along with Typescript. I'm facing an issue where I am not receiving any errors when an interface does not align with the specified types for that interface. Although my IDE provides aut ...

What are the steps for utilizing the watch feature in Vue.js with TypeScript?

Currently, I am looking to convert this JavaScript script into TypeScript. However, I require the syntax for watchers. export default { props: ['branch_id'], watch: {} } ...

Angular Bootstrap Modal not Displaying

<img id="1" data-toggle="modal" data-target="#myModal" data-dismiss="modal" src='assets/barrel.jpg' alt='Text dollar code part one.' /> <div id="myModal" class="modal fade" *ngIf="isModalShowing"> <div class=" modal-lg ...

Vue: event triggers malfunctioning and components unresponsive

I am new to Vue.js and I'm attempting to trigger an event from my grand-child component (card) to the child component (hand) and then to the parent component (main): card (emit play event) => hand (listen for play event and emit card-play event) => ...

Tips for successfully sending a parameter with InertiaJS

After setting up a route and calling it through Inertia, I encountered an issue with how the URL appeared in the browser. The route Route::get('/blogs/{post}', [BlogController::class, 'show']); in web.php functions correctly when input ...

How can I configure nest.js to route all requests to index.html in an Angular application?

I am developing an Angular and NestJS application, and my goal is to serve the index.html file for all routes. Main.ts File: async function bootstrap() { const app = await NestFactory.create(AppModule); app.useStaticAssets(join(__dirname, '..&ap ...

Utilizing the $set method to capture a jQuery variable and append it to a Vue array object

Currently, I am retrieving data from an API using jQuery's getJson method to extract the information. After successfully obtaining the data, my aim is to assign it to a Vue array object by making use of app.$set. Although I have managed to extract an ...