Reactive property in the Vue composition API

Within a Vue 3 project that utilizes TypeScript, there are two properties named locale and content:

<script setup lang="ts">
import { computed, ref } from 'vue'
import { useI18n } from "vue-i18n"

import { Landing, Locales } from "@types";

interface Props {
  item: Landing;
}

const props = defineProps<Props>();

const i18nLocale = useI18n();

const locale = computed(() => {
  return i18nLocale.locale.value as unknown as Locales
})

const content = computed(() => {
  return props.item.content[locale.value]
})

// const locale = ref(i18nLocale.locale.value as unknown as Locales);
// const content = ref(props.item.content[locale.value]);
</script>

When the value of i18nLocale.locale is updated, my computed locale changes along with my content. This functionality works properly.

However, if I attempt the same thing using the commented-out line with ref(), nothing gets updated. Is it necessary to use computed, or would using ref() be sufficient?

Answer №1

The lines of code provided establish references with initial values. These references can be manually altered, but they lack a connection to the original data source and cannot respond to changes in that data.

In the case of locale, there is no need for a computed property as i18nLocale.locale is already a reference. You can directly utilize it or assign it to a variable if you prefer not to use i18nLocale:

const locale = i18nLocale.locale

On the other hand, content must be a computed property because its value needs to be recalculated when dependencies like locale and item.content change.

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

Navigating the From and To routes in Nuxt/Vue: A comprehensive guide

I am currently working on a Nuxt/Vue project. While inspecting in Dev Tools, I came across a From and To property. How can I access these properties within the Nuxt application? I have attempted to use this.$nuxt.$route, this.$nuxt.$router, and this.$rou ...

Error occurs in production environment in Vite 2 when the ref element is undefined while using the Composition API

While working with Vue3 and the composition API, I encountered an issue where the ref element remains undefined after building my project. I tried reproducing the problem and it seems like I might have used it incorrectly, but the reason behind this behav ...

Sass Alert: The mixin called roboto-family is missing from the stylesheet. Trace: Problem detected in src/pages/forms/forms.scss at

Greetings, I am diving into the world of Ionic for the first time. Recently, I embarked on a new project in Ionic and decided to integrate a theme. To do so, I copied an .html file, an .scss file, and also created a .ts file. Forms.html <!DOCTYPE html ...

Contact creation not working on non-HubSpot form popups

I'm currently experiencing an issue with my website that has non-Hubspot forms. We have successfully integrated the tracking code to generate cookies for users, track their sessions, and enable the non-Hubspot forms. However, we are facing a problem s ...

Navigating the reactive array in Vue 3 with finesse

Currently, I'm facing some challenges with my Vue 3 app in terms of getting things to work as expected. The main issue lies in properly accessing a reactive array. Let me provide you with the code for better comprehension: Within my global store sto ...

Adjust the panel size accordingly for smaller screens

My application is utilizing the Spotify API to retrieve names and images. These are then displayed on my webpage within cards/panels in the following format: <div class="col-md-4" v-if="type == 'tracks'" v-for="(track, index) in tracks"> ...

Is there a way to adjust the width of the b-form-select component?

My issue lies with the b-form-select element, as it defaults to a width of 100%. I attempted adjusting the width like so, but unfortunately it did not have the intended effect. <b-form-select v-model="xxx" :options="yyy" width: 50% ...

Embed Vue applications within the container of the main Vue application

My goal is to establish a foundational Vue application that offers essential features such as signing in, navigating with a sidebar, and the flexibility to interchange navbar items. I envision creating separate Vue applications for each navbar item. Main ...

Steps for initiating an Angular 4 project

While most developers have moved on to Angular 5, I was tasked with creating a project using Angular 4. After conducting research for several days, I discovered that downgrading the Angular CLI would allow me to accomplish this. By following this approach, ...

Steps for incorporating ProxyConfig in Angular7 Application1. First, create a new

Having trouble building the application with proxy configuration. It works fine with ng serve or npm run start, but I need it to work with npm run build or ng build. After that, I want to deploy the dist folder to Tomcat webapps and make everything functio ...

Strategies for avoiding interruption of the dragenter event while dragging over a nested element

I'm currently developing a personalized drop zone feature. Below is the HTML code snippet: <template>   <div class="dropzone"     @drop.prevent="dropFileHandler"     @dragover.prevent="dragOverHandler" ...

What is the best approach to have a method in the parent class identify the type based on a method in the child class using TypeScript?

I'm faced with a code snippet that looks like this. class Base{ private getData(): Data | undefined{ return undefined } public get output(): Data | undefined { return { data: this.getData() } } } class ...

TypeScript issue encountered with parseInt() function when used with a numeric value

The functionality of the JavaScript function parseInt allows for the coercion of a specified parameter into an integer, regardless of whether that parameter is originally a string, float number, or another type. While in JavaScript, performing parseInt(1. ...

Employing Vue's test-utils along with jest to test the accuracy of a particular input element's value

When using Vue test-utils, I am trying to test the value of a specific input element that is populated through a prop. I have managed to locate the element by its id: it("Address should render Street 1", () => { const street1 = wrapper. ...

Troubleshooting a problem with arrays in Vue.js and axios

Hey everyone, I recently received a response from an axios call that looks like this: axios.post('/api/hr_employee/days/'+ this.period_data.year +'/'+ this.period_data.month +'?page='+this.currentPage+'&api_token=&apo ...

Facing an issue with displaying a component error in a mat-form-field in Angular 9

I am looking to develop a shared component for displaying errors in Angular Material. Here is my shared component pfa-share-error: <mat-error *ngIf="fieldErrors(fieldName).required && fieldErrors(fieldName)"> Required </mat-err ...

Encountering an error with the Typescript 'any' type in Ionic 2

I need help understanding an error I encountered: EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in build/pages/search/search.html:17:14 ORIGINAL EXCEPTION: Cannot find a differ supporting object 'function () { return [ { ...

Utilizing Observable for Navbar concealment in Angular 2

Here's my dilemma: I have a navbar in my app.component that I want to hide before someone logs in by setting a boolean variable to true using ngIf. app.component.html: <navbar *ngIf="_userLoggedIn === true" ></navbar> <router-outlet& ...

What is the significance of default parameters in a JavaScript IIFE within a TypeScript module?

If I were to create a basic TypeScript module called test, it would appear as follows: module test { export class MyTest { name = "hello"; } } The resulting JavaScript generates an IIFE structured like this: var test; (function (test) { ...

Ways to collect particular tokens for delivering targeted push notifications to designated devices

When filtering the user's contacts, I ensure that only contacts with created accounts are displayed on the screen. This process helps in visually organizing the contact list. List<PhonesContacts> phoneContacts = snapshot.data; Lis ...