I am looking to update Vue.js 3's reactive values by utilizing a method implemented in an external file

Exploring how to update reactive values in Vue.js 3 using a method from an external file.

Currently experimenting with Vue.js 3, I am looking to pass reactive values to a method defined in an external file for updating purposes.

During the testing phase of creating a count component, I encountered a situation where the value updates successfully in the increment1 method but not in the increment2 method. What changes are needed to ensure the value updates in both methods?

Related Code Snippets

<script setup lang="ts">
import { Ref, ref } from 'vue';
import { increment as increment2 } from './increment';

const count = ref(0);
const increment1 = () => {
  count.value++;
}

</script>

<template>
  <main>
      <p>Counter Value</p>
      <p>{{ count }}</p>
\      <button @click="increment1()">Action1</button>
\      <button @click="increment2(count)">Action2</button>
  </main>
</template>
export const increment = (count: number) => {
  count++;
}

FW/Tool versions:

Vue.js 3.2.27 TypeScript 4.7.4

Answer №1

When utilizing refs in the template, they are automatically unwrapped. This means that your line of code:

@click="increment2(count)"

is essentially equivalent to:

@click="increment2(count.value)"

This approach is not reactive. To achieve reactivity, you need to pass the actual count ref, which can only be accomplished within your <script> tag.

import { increment } from "./increment.js";

const increment2 = () => {
  increment(count);
};
<button @click="increment2">Action2</button>

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

Assign the default value of a Vue prop to the options of its parent component

I have a child component that needs to accept a value given in the component's parent's $options object as a possible default value. In the background, the component should be able to receive its data through a prop or from a configuration. Howe ...

Retrieve unique values based on two or more criteria from an array in TypeScript

Presented here is an array. https://i.stack.imgur.com/zHXim.png The desired pattern is: https://i.stack.imgur.com/EGUiM.png I am interested in retrieving unique "CityRef" and "City". Subsequently, locating all items associated with them. ...

What is the reason behind the return type of 'MyType | undefined' for Array<MyType>.find(...) method?

Currently, I am in the process of developing an Angular application using TypeScript. As part of this project, I have defined several classes along with corresponding interfaces that align perfectly with their respective properties: Map: export class Map ...

Checking if the slot has no content or is empty

I recently created a small Loading component with the default text set to 'Loading...'. I thought using slots was a good idea, so here is what my template looks like: <p class="loading"><i class="fa fa-spinner fa-spin"></i>< ...

typescript in conjunction with nested destructuring

ES6 has definitely made coding more efficient by reducing the number of lines, but relying solely on typescript for everything may not be the best approach. If I were to implement type checking for arguments that have been destructed multiple levels deep, ...

Tips for shrinking the Docker image size of a Nuxt application

I'm currently deploying my Nuxt app using Docker, but my Docker image size is currently at 260MB. I've made some efforts to reduce its size by utilizing the Node Alpine base image. Below is the content of my Dockerfile: FROM node:10-alpine RUN ...

Please provide pattern block links and any special characters as input

I have a promotion box where users can input text to send to administrators. Objectives I need to prevent users from inputting links and special characters. Users should only be able to input letters a-z (case insensitive) and numbers 0-9. Input is option ...

Numerous mistakes detected in the TypeScript code

I've implemented the following class within an ASP.NET Core React application: import * as React from 'react'; interface MyInputProps { inputType: string; id: string; className: string; parentFunctio ...

The specified property is not found in type (2339)

I encountered a TypeScript error stating Property 'UPDATE_COLUMNS' does not exist on type 'DimensionAction'.ts(2339), even though it is clearly defined above. This issue occurred while using VSCode. I'm starting to suspect it may ...

What is the best way to transmit data via $router.push in Vue.js?

I am currently working on implementing an alert component for a CRUD application using Vue.js. My goal is to pass a message to another component once data has been successfully saved. I attempted to achieve this by passing the data through $router.push lik ...

Utilizing Typescript's compilerOptions.outDir for efficient compilation with external non-TS modules

I am encountering an issue with non-ts modules (text assets) not being transferred to the outDir as specified in tsconfig.json (or I might not be performing the task correctly). Here is a simple example to reproduce the issue: // /src/main.ts import text ...

When attempting to utilize the Angular2 http.get function to retrieve data from a Restful Webservice, I encountered an issue where the error status 200 was returned

Angular 2 Issue with http.get Request to Restful Webservice When I try jsontest.com, I receive the value properly. getCurrentTime() { //return this.http.get('http://date.jsontest.com') return this.http.get('http://localhost:80 ...

Can you please provide instructions on how to obtain the TypeScript definitions file for a specific version of Jquery, such as 3.2.1

As I navigate my way through TypeScript, I find myself in need of accessing type definitions for a jQuery project in Visual Studio. The project currently utilizes jquery version 3.2.1, and I'm on the lookout for TypeScript type definitions for it. Af ...

Using Vue JS to display information conditionally within a single component

I currently have a component that dynamically displays the data from an array of objects. However, I want the component to display only one object based on a specific condition. Is it possible to show either one object or another depending on the value o ...

Error message in Vue3 with TypeScript: When using global components, you may encounter the error "JSX element type '___' does not have any construct or call signatures.ts(2604)."

Each globally registered component suddenly displays a red TypeScript squiggle in the markup! Surprisingly, the app continues to function without any visible errors! This issue arises with all components, whether they are custom-made or third-party libr ...

Exploring the functionality of incorporating double curly braces from Vue.js within a PHP string parameter

As I combine Vue.js, PHP, and HTML, I have encountered a small problem. In the HTML section below: <button class="task button is-fullwidth is-flex" v-for="task, taskid in list.list"> <span class="icon"> ...

Issues with FUNCTION_INVOCATION_FAILED error encountered during deployment on Vercel and Nuxt

Just imported a Nuxt project from GitHub that includes the vercel.json config: { "version": 2, "builds": [ { "src": "nuxt.config.js", "use": "@nuxtjs/vercel-builder" ...

Issues with the anchor tag's CSS styling arise when converting it to a router-link within a Vue.js

<div class="col-xl-8 col-lg-8 col-md-12 col-sm-12 col-12"> <!-- navigations --> <div class="navigation"> <ul> <router-link tag="li" to="/" >Home</router-link> <router-link tag="li" to ...

What steps are involved in setting up a Typescript-based custom Jest environment?

Currently, I am attempting to develop an extension based on jest-node-environment as a CustomTestEnvironment. However, I encountered an error when trying to execute jest: ● Test suite failed to run ~/git/my-application/tests/environment/custom-test ...

What is the process for clearing cache in inversifyJS?

In my simple application, I am using express server along with TypeScript. Initially, I can successfully reach my endpoint, but after updating the model, the old data persists. I have attempted the suggested approach mentioned here: How to disable webpage ...