Encountering the "v_isRef": true flag while utilizing Vuex within a composition function

I encountered an unexpected outcome when using the loginString() function in my template. It seems that there is a need to include .value in templates, which I thought wasn't necessary.

{ "_dirty": true, "__v_isRef": true, "__v_isReadonly": true }

Below is the code snippet:

App.vue

<template>
    <div>{{ language }}</div>
    <div>{{ login }}</div>
    <div>{{ loginString() }}</div> <!-- Issue is here -->
</template>

<script setup>
    import { computed } from 'vue'
    import store from './store.js'
    import useLanguage from './useLanguage.js'

    const { loginString } = useLanguage()

    let language = computed(() => store.state.language)
    let login = computed(() => store.state.languages[language.value]['translations']['Login'])
</script>

and useLanguage.js

import store from './store.js'
import { computed } from 'vue'

function useLanguage()
{
    function loginString()
    {
        let language = computed(() => store.state.language)
        let login = computed(() => store.state.languages[language.value]['translations']['Login'])

        return login
    }

    return { loginString }
}

export default useLanguage

An my store.js

import { createStore } from 'vuex'
 
export default createStore({
    state()
    {
        return {
            language: 'en',

            languages: {
                "es": {
                    "lang_short": "es",
                    "lang_long": "espanol",

                    "translations": {
                        "Login": "Benga, venga!",
                        "Welcome Back": "Opa! Muy amigos!",
                    }
                },

                "en": {
                    "lang_short": "en",
                    "lang_long": "english",

                    "translations": {
                        "Login": "login",
                        "Welcome Back": "Welcome",
                    }
                },
            },
        }
    },
})

Answer №1

When you return a root level ref from the setup() hook in Vue, it is automatically unwrapped. For more information on this behavior, check out this issue and the related commit.

Now, only the top-level refs get automatically unwrapped when returned from the setup() function.

If your loginString value returned from setup is a function instead of a ref, remember that if that function returns a ref or a computed property, you need to access it as loginString().value in your template...

Answer №2

After exploring the Vue 3 documentation, I came across the solution. To access the actual value, I realized that using .value or unref was necessary to unwrap the ref.

import dataStore from './store.js'
import { computed } from 'vue'

function useLanguage()
{
    function translateText(string)
    {
        let selectedLanguage = computed(() => dataStore.state.language)
        let translatedString = computed(() => dataStore.state.languages[selectedLanguage.value]['translations'][string])

        return translatedString.value
    }

    return { translateText }
}

export default useLanguage

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

What is the best way to choose the initial element in every group using jQuery?

If we have the following HTML input: <p>A</p> <p>B</p> <p>C</p> <div>D</div> <p>E</p> <p>F</p> <p>G</p> We can select B, C, F and G using the following code: $('p + ...

Problems with select box rendering in Internet Explorer with Materialize CSS

When using materializecss select box in Internet Explorer 9 or higher, scrolling with the mouse button is not working. You must click on the scroll bar inside the select box for it to scroll. This issue does not occur in other browsers. I have attached a s ...

Is there a way to invoke JavaScript functions from external files in an EJS template?

I've got this new Ship file to add. The script that populates the fleet dropdown menu is working perfectly: new.ejs file: <% include ../partials/header %> <div class="container"> <div class="row"> <h1 style="text-al ...

Remove a specific date entry from the database using VUE JS and Axios

I am encountering a challenge with Vuejs as I work on a To-Do list. The tasks need to be stored in a MySQL database, and I also want the ability to delete tasks. While I have succeeded in importing and creating data, I'm facing difficulty in deleting ...

Translating from a higher-level programming language to a lower-level programming language

Is compilation effectively the transformation of high-level programming languages (HLL) into machine code or low-level language? If so, why is TypeScript (a HLL) compiled to JavaScript (also a HLL) instead of being compiled to a low-level language? ...

What could be causing my token to not save after registering a user?

I am currently facing an issue when submitting a registration form. While the user is successfully created, the token is not stored in localStorage, which prevents me from being redirected immediately to the app and forces me to log in again. Here is my R ...

Having trouble getting npm install to add any libraries? Wondering how to fix this issue?

Currently, I am using Node version 14.15.5, npm version 6.14.11, and working on VS Code. I attempted to install two packages with the following commands: npm install express npm install lodash Unfortunately, neither installation was successful. This ...

Is it possible to create a looped GLTF animation in three.js using random time intervals?

I am currently exploring the world of game development with Three.js and have a specific query regarding animating a GLTF model. Is it possible to animate the model at random intervals instead of in a continuous loop? In the game I am creating, players wil ...

Tips for customizing the appearance of a child component using the parent component in Vue.js

I am working on creating a layout using Vuejs and flexbox that includes elements like 'header, sidebar, main-content, sidebar, footer'. I have separated each part of the layout into individual .vue files, such as header.vue and sidebar.vue. In ...

How can I obtain the model values for all cars in the primary object?

const vehicles={ vehicle1:{ brand:"Suzuki", model:565, price:1200 }, vehicle2:{ brand:"Hyundai", model:567, price:1300 }, vehicle3:{ brand:"Toyota", model ...

The checkbox is not updating as anticipated

I'm currently developing a basic widget that allows the user to change the value of either the check box OR the entire div by selecting them. Below is the code I have implemented: $(document).ready(function() { $(document).on("click", ".inputChec ...

What could be causing me to not receive the prepackaged error messages from Angular in my WebStorm 8?

Having some trouble here... my angular errors are always so cryptic, like this: I usually manage to figure out the issue on my own, but I'm really hoping someone can offer guidance on how to get those nice error messages that angular supposedly displ ...

Tips for transferring the present value of a form input to a Vue component property?

I've got a functional HTML signup form (created with Laravel) that's working smoothly. Now, I'm looking to spice it up by adding password requirement checkboxes through a Vue component, as illustrated here: https://i.sstatic.net/zhZF4.png ...

Having trouble connecting to the http json service in AngularJS

I recently started learning angularjs and I'm facing an issue with my code. There seems to be a flaw in the code and I'm uncertain about it. From a java perspective, the httpController has a nested function defined inside. Below is the code sn ...

Tips for accessing child elements within an Angular component

I'm struggling to get a reference of child elements within a component. I have experimented with ElementRef, TemplateRef, QueryList, ViewChild, ViewChildren, ContentChild, and ContentChildren without success. <app-modal> <section #referenc ...

Implementing multiple filters with jQuery

Make a Selection `<select class="form-control" id="technology"> <option name="sort" value="2g" id="2g"gt;2G</option> <option name="sort" value="3g" id="3g"&g ...

find the middle element in the Vue array

Currently in the process of developing a custom Vue carousel component, I have utilized some code snippets from this resource: link My goal right now is to enhance the slider with additional navigation bullets. However, due to the justify-content:center p ...

How can I add a new property to an object type within an Interface in TypeScript?

I'm currently exploring how to merge declare an interface, with the twist of adding a property to the object literal type instead of directly to the interface itself. Within a library, I have a type that looks like this: interface DefaultSession { ...

Generating hills with PlaneGeometry in Three.js

Currently, I am searching for a straightforward technique to generate non-uniform hills in Three.js. By utilizing particles and positioning them with a sine wave algorithm like the following snippet: x = Math.random() * 1000 y = Math.sin( x / freq ) * ...

What is the best way to generate a linked list from a JSON array?

I have a list of universities that I generated from a JSON file and now I want to create hyperlinks for each university in the list so that users can navigate to their respective university pages. HTML <ul data-bind="foreach: university"> <li ...