Updating a variable in Nuxt 3 using vue.js

Recently, I started experimenting with Nuxt 3 and Vue 3. All I want is a simple value change when clicking on my div tag.

<a id="change" @click="changeValue()">{{value}}</a>
<script  lang="ts" setup>
let value = "Old";
function changeValue(){
  value="new"
}
</script>

This approach is the only one I have attempted so far.

Answer №1

Rap's response is spot on and demonstrates the utilization of Options API successfully. In addition, here's another approach utilizing Composition API:

  • Begin by defining the value variable as a reactive variable using Ref(). Ref has the ability to store any data type.
  • To modify the reactive variable, make use of the .value property.
<template>
  <div id="change" @click="changeValue">{{myValue}}</div>
</template>

<script setup>
const myValue = ref("Old");
function changeValue(){
  myValue.value = "new"
}
</script>

Please note: In the provided example, I have changed the variable name from value to myValue to prevent confusion with the .value property.

If you wish to delve deeper into this topic, consider exploring the Reactivity in Vue.

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

Combining default and named exports in Rollup configuration

Currently, I am in the process of developing a Bluetooth library for Node.js which will be utilizing TypeScript and Rollup. My goal is to allow users to import components from my library in various ways. import Sblendid from "@sblendid/sblendid"; import S ...

Not entirely sure of the purpose of this code? I'm looking to get it to perform the exact opposite of its current functionality

I've been assigned a project that someone else started, and I'm having trouble understanding how this specific part of the code functions. It seems to be doing the opposite of what I want it to do. Currently, when I use a select multiple and pre ...

Storing two values when an option is selected in a dropdown in Javascript using the map functionIn this tutorial,

The JSON data provided is structured as follows: [ { "course_name": "React", "course_id": 2 }, { "course_name": "Python", "course_id": 1 } ] Displa ...

Listening to Azure Service Bus messages using NestJS

I have a unique service that manages the business logic for my product. Whenever a message is received through the Azure service bus, I need to call this service. To retrieve messages from the Azure service bus queue, we are utilizing the Azure npm packa ...

Oops! TypeScript error TS2740: The type 'DeepPartial<Quiz>[]' is currently missing key properties from type 'Question': id, question, hasId, save, and a few more

I'm struggling to resolve this error. Can anyone provide guidance on what needs to be corrected in order for this code to function properly? import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm& ...

No error was flagged when the function had the potential to return undefined

getStage may sometimes return undefined without reporting any errors, which could potentially lead to a code crash. const a = Math.random() > 0.4 function getStage(): string { if(a) { return '' } } c ...

Setting a condition for a function call when a checkbox is clicked

My table has columns labeled NoBill and Bill, with checkboxes as the values. Here is the default view of the table: https://i.stack.imgur.com/ZUvb2.png When the checkbox in the NoBill column is clicked, the total value (this.total) is calculated. The t ...

Having trouble locating the OBJ file in your Three.js WebGL project?

I am attempting to load an obj file using Three.js, but despite following various tutorials and resources online, I am only met with a black screen and no error messages in the console. The console output from the LoadingManager indicates that the objects ...

An effective way to prevent right-clicking on iframes across all websites

I am facing an issue with disabling right click for the iframe. I've successfully disabled it for the default URL of the IFrame, but when displaying any other webpage, the right click remains usable. Below are the sample codes I have used: document.o ...

Angular's ngRoute is causing a redirect to a malformed URL

Currently, I am in the process of developing a single-page application using AngularJS along with NodeJS and Express to serve as both the API and web server. While testing locally, everything was working perfectly fine. However, after cloning the repositor ...

discovering obscured information using jquery

I am working on a code snippet where I need to hide the detailed content for display purposes and only show it during printing: <div> <ul> <li> <span style="font-size: 1.25em;"> <strong> ...

What steps should I take to ensure that the array yields the correct output?

Why is my code not creating an array of [0, 1, 2] when I pass the number 3 as a parameter? const array = [0]; const increment = (num) => { if (num > 0) { increment(num - 1); array.push(num); } return; }; console.log(array); incremen ...

Error in Formatting Labels in CSS

I currently have a form with textboxes that include validation. Everything works smoothly when clicking the save button for the first time, but if we fill out the textboxes and then remove the text, an error message is displayed on the textboxes. You can v ...

Determine the output of a function based on the structure of the input parameter by mapping through a complex nested object

Trying to implement some intricate typing for a project I'm developing, and wondering if it's achievable with TypesScript. The project in question is a form generator based on schemas and promises, using Vue and TS. It handles UI rendering, vali ...

My attempt to adjust the word form based on the quantity is not functioning as expected

Having trouble making a noun plural based on a count and need some help understanding the issue. // PLEASE DO NOT MODIFY THE FOLLOWING INPUTS. const noun = prompt("Enter a noun"); const count = prompt("Enter a number"); console.log(nou ...

Vue JS sorting elements and reverting back to their original state

Within my Vue application, I have a list of results that I want to sort alphabetically after performing a search. However, I also need the ability to revert back to the original order, which is based on relevancy in my case. The 'relevancy' orde ...

Electron / Atom Shell unable to locate specified module

I am completely new to npm, node, and Electron. Here is the structure of my folder: -package.json -index.html -main.js -js/myStuff.js -node_modules In the file myStuff.js, I have a line that says var chokidar = require('chokidar'); but it keep ...

What is the reason for TS expressing dissatisfaction about the use of a type instead of a type entry being provided

Below is a snippet of code for a Vue3 component that takes in an Array of Objects as a prop: <script lang="ts"> interface CorveesI { What: string, Who: string, Debit: number } export default { name: 'Corvees', props: { ...

Struggling with React Native and WebSocket integration issues

I've heard some concerns about Socket.io not functioning properly in React Native, so I opted to utilize plain WebSocket instead. Setting up a WebSocket server with node.js was relatively straightforward for me. While all my attempts were successful ...

Page freezing due to JQuery scroll event

I successfully implemented a trigger that checks if an element is within the visible viewport and added it to the scroll event. While this works smoothly on some pages, I noticed that on larger pages it causes the page to freeze. In Firefox, I experienced ...