What is the reason for the quotes appearing around a ref() string?

Examining this code snippet, we see a string displayed that represents the content of a ref()

<script setup>
import { ref } from 'vue'

const msg = ref('x')

const asyncCall = () => {
  const result = ref('one')
  setTimeout(() => {
    result.value = 'two'
  }, 2000)
  return result
}

msg.value = asyncCall()

</script>

<template>
  <h1>{{ msg }}</h1>
</template>

What causes the quotation marks around the displayed value ("one" and "two")?

Displayed below is an animated screenshot illustrating how "one" transforms into "two" when the window is reloaded.

https://i.sstatic.net/FZjgc.gif

Answer №1

To resolve the issue, you can simply change `return result.value` to return `.value` when returning the result:

 return result.value

Although this method works well, I personally prefer utilizing a Vue approach like using computed properties or watch:

<script setup>
import { ref , watch} from 'vue'

const msg = ref('x ')
 const result = ref('one')

const asyncCall = () => {
  setTimeout(() => {
    result.value = 'two'
  }, 2000)
 
}
asyncCall()

watch(result,(v)=>{
  console.log(v)
  msg.value=result.value
},{
  immediate:true
})
  
</script>

<template>
  <h1>{{ msg }}</h1>
</template>

Answer №2

Objects and arrays remain as is in templates without being unwrapped. Here's an example:

import {computed, ref} from "vue"

const object1 = [
  computed(()=> "some thing"),
]

const object2 = ref([
  computed(()=> "some thing"),
])
//Both, object1[0] and object2[0] will display "some thing", with quotes.

To access the computed value:

{{ object1[0].value }}

In current version [Vue 3.4], when you output the whole computed object, it runs through JSON.stringify(), resulting in the quotes.

  • Summed up:
return.value

Useful Links:

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

Discover the steps to implement a live user list in a chat application with the help of angular.js, socket.io, and node

Currently, I am in the process of developing a chat application with AngularJS and Socket.io. The current status of my project allows users to send and receive messages from different individuals. To gain access to the chatbox, users need to input their na ...

Using a loop to iterate through a multidimensional array in Node.js or JavaScript, creating additional data and adding new key-value pairs

Here is an array of objects showcasing different intents and results : var finalresult = [{ "Date": "Wed Jan 15 2020 00:00:00 GMT+0530 (India Standard Time)", "data": [{ "intent": "delivery", "result": [{ "h ...

What's the best way to dynamically show Bootstrap collapse panels in a loop with AngularJS ng-repeat?

Currently, I am utilizing angularJS and attempting to include a bootstrap collapsible-panel within a loop. The code that I have written is causing all the panel bodies to be displayed beneath the first panel header. I need each body to be shown below i ...

Using THREE.js: Object3D Dimension Shrinkage

Is there a way to disable sizeAttenuation for an Object3D in THREE.js? I'm working on rendering a trajectory at a massive scale using arrow helpers to show the motion direction. I want the arrow heads to maintain their orientation without changing si ...

Issue with the binding of ng-options in AngularJS is unresolved

I'm encountering difficulties trying to update a select element in the view when changes occur in the model. In my implementation, I am utilizing ng-options as shown below. <select ng-options="item as item.empName for item in employees track by it ...

Automatically create subclasses and reclassify object lists with CanJS

My CanJS script receives a list of results from a remote JSONP server in this format: [ { "class": "ABaseClass", "value": "1"}, { "class": "ASubClass", "value": "2"}, { "class": "ABaseClass", "value": "3"}, { "class": "ASubClass", "value": ...

I am experiencing difficulty running Vue.js projects that I downloaded in Visual Studio Code and keep encountering the following error message: ERR! For a detailed log of this issue, please refer to:

After restarting my computer and reinstalling everything, I encountered an issue. I can create a Vue project without any problems and run it using the npm run serve command. However, when I try to run the projects that I have archived, I receive the follow ...

I encounter issues with my fetch request as it is denied while attempting to access the HTML content from the specified

I'm currently working on a project in my express app where I need to retrieve the html code of multiple urls. However, I keep encountering this error: reject(`new FetchError(request to ${request.url}` failed, reason: ${err.message}, 'system' ...

Easily integrate an HTML form as an input for your Python script

Despite my thorough searches, I haven't been able to find a satisfactory answer. I am creating a local HTML page with Python and would like to extract data from a form on that page, use it as input for a Python script, and then properly display the r ...

Utilize JavaScript to Trigger AJAX HoverMenuExtender in .NET

Within my C# web application, I am attempting to trigger an Ajax HoverMenuExtender using JavaScript, rather than relying on hovering over a designated control. When I set the TargetControlID of the HoverMenuExtender to a control on the page and hover ove ...

In Android, when you invoke a PHP file that contains JavaScript code, the result of the call is the execution of the JavaScript code

I am developing an Android application and facing a barrier. The issue at hand is that one activity in my application sends the user's current location to the database. Using this information, I reference another table in the database containing other ...

Develop an innovative showcase page showcasing 151 individual profiles with React Router

I am new to using React Router and feeling a bit confused about how to proceed. On my homepage, I have 151 unique monster thumbnails. When a user clicks on a thumbnail, they should be directed to the specific monster's 'show page'. Currently ...

Insert the URL into either a div or an iframe

It seems like a common issue. I've come across multiple solutions for this problem. using jquery load using iframe I attempted both methods but encountered difficulties in loading content. Specifically, I tried to load google.com and it didn't ...

Tips for merging arrays conditionally in JavaScript

Currently facing a dilemma without a clear solution, aiming to provide minimal context and focus solely on the problem at hand A worker logs their daily hours at work, stored in the "dayli_data" array. If the worker misses logging hours, it triggers the " ...

include a new key value pair in the response object

$.post("./bigData.php", {'username': username}, function(response) { console.log(response); var bigUser = response[username]; // first attempt var bigUser2 = username.response; // second attempt }); W ...

Enhancing Vue JSX functionality: Tips and strategies

When working with JSX in Vue, it requires creating a dedicated file, whereas in React, we can use it inline within JavaScript. Is there a way to achieve this in Vue? Contents of .babelrc : { "presets": [ "@babel/preset-react&quo ...

single calendar bootstrap-datepicker allowing users to select date ranges

Is it possible to create a single calendar range date picker with Bootstrap instead of using two separate calendars? Currently, I have implemented a solution with two calendars. $('#datepicker').datepicker({ }); <link href="https://cdnjs.cl ...

Setting environmental variables throughout your application using create-react-app on the front end

Hey there! I have a simple Todo app with the client using create-react-app and the server running on node. I'm struggling to properly set environment variables across my app. I've placed my .env file in the root directory of the app, which curre ...

ExtJS variable not populating with servlet data

I'm facing an issue with my code where I am calling a servlet from JavaScript using an AJAX request. The data from the servlet is shown in a message box within the success function, but it's not being loaded into a variable called `myData` in Jav ...

Is it possible to apply styles to the body of a document using styled-components?

Is it possible to apply styles from styled-components to a parent element, such as the <body> tag, in a way that resembles the following: const PageWrapper = styled.div` body { font-size: 62.5%; } ` ...