Challenges arising from the usage of Vue component state in TypeScript

I'm encountering an issue with a basic Vue component. I'm attempting to trigger a rerender of v-if="isTouched" by setting the setter (via the touch event). Vue dev tools indicate that the _isTouched variable is showing as "undefined". My understanding is that variables like this should be automatically included in the components data() block. However, this doesn't appear to be the case. Any insights on what I might be overlooking?

Thanks//M

<template>
    <div v-if="selectedProgramItem">
        {{isTouched}}
        <span v-if="isTouched">Unsaved changes</span>
        <input type="text" class="form-control" v-model="selectedProgramItem.title" />
        <button @click="touch()">Touch object</button>
    </div>

</template>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import { ProgramItem } from '@/entities/ProgramItem';

@Component
export default class ProgramEditItemPane extends Vue {
    @Prop() private selectedProgramItem?: ProgramItem;

    _isTouched: boolean = false;

    private get isTouched(): boolean {
            return this._isTouched;
    }
    private set isTouched(val: boolean) {
        this._isTouched = val;
        // this._isTouched <<<<<<<<< == undefined here.
    }
    private touch() {
        this.isTouched = true;
    }

}
</script>

Answer №1

Referenced in this Github Issue Comment:

Underscore prefixed properties are reserved for Vue's internal use.

This information is also available on this page:

Properties that start with _ or $ won't be directly accessible on the Vue instance to avoid conflicts with internal properties and methods. Access them as vm.$data._property instead.

To resolve the issue, rename _isTouched to something like isTouchedField.

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

The type 'Observable<void | AuthError>' cannot be assigned to 'Observable<Action>'

I am encountering an error that reads: error TS2322: Type 'Observable<void | AuthError>' is not assignable to type 'Observable<Action>'. Type 'void | AuthError' is not assignable to type 'Action'. Type &a ...

Issue with Vue.js: Difficulty sending an array of values to an endpoint

I am currently in the process of learning Vue in order to complete my project, which has a Java Spring backend. The endpoint I am working with expects an object with the following values: LocalDate date; Long buyerId; Long supplierId; List<OrderDetails ...

Matching the appropriate data type for interface attributes

In the process of developing a module (module1), I have defined the following interface type: interface ModuleOneInterface { keyOne: customInterface; keyTwo: customInterface; keyThree: customInterface; } Now, as I work on another module (modul ...

Enhance your TypeScript React development with NeoVim

As a newcomer to vim, I decided to test my configuration with react and typescript. To do this, I created a simple demo app using the command npx create-react-app demo --template typescript. Next, I opened the directory in neovim by running nvim .. However ...

Creating a Modal using Typescript with NextJS

Currently, I'm working on creating a modal within my app using NextJS with Typescript. Unfortunately, I've been struggling to eliminate the warning associated with my modal selector. Can someone provide guidance on how to properly type this? cons ...

"Exploring the method of exporting a function in Vue.js 3 using the Composition API

// Inside File2.vue <script> import { ref, computed } from 'vue'; let example = ref(1); const totalSum = computed(() => { let result = 5 + 5; return result; }); </script> <template> <div class="pa ...

Loop through a collection of unique identifiers for documents and establish event listeners in Firestore

Within my Vuex store, there is an action designed to retrieve a list of uids for followed users from the current user's Firestore UserDataCollection document. The action then processes each uid to extract data that will be displayed on the UI. While t ...

how to switch page direction seamlessly in vue without triggering a page refresh

I'm looking to update the page direction in Pinia whenever a value changes, and while my code is functioning correctly, it reloads the page which I want to avoid. Take a look at my App.vue <script setup> import { useaCountdownStore } from " ...

Steps to assign a JSON file to an array within an object in Angular

What is the best way to assign a JSON file to an array within my "Client" object? I currently have a JSON file named Clients.json with the following structure: { "clients": [ { "firstName": "nameA", "lastName": "lastA", "doctorsNam ...

Leverage the power of TypeScript by enabling the noImplicitAny flag when working

Currently, I am looking to activate the noImplicitAny flag in my compiler. My main issue lies with utilizing lodash/fp as there are no typings available at this moment. Due to this, the compiler is generating errors due to the absence of a definition file ...

Learn how to send an SMS using Angular and Ionic 4 without having to open the native SMS app

I have been actively monitoring the GitHub repository for the Ionic Native SMS plugin at https://github.com/cordova-sms/cordova-sms-plugin. Following the suggested configuration from the repo, I have set it up as follows: var options = { repla ...

Using Vue to Send Component Data to the User Interface

I am exploring Vue and trying to create a website with a Facebook login feature. I have a Facebook login component that is functioning properly, but I am struggling to make the acquired fbid and fbname available outside of the component in my Vues. I under ...

What is causing the "excessive stack depth" error in this JSX code?

Exploring Next.js' TypeScript (4.2.3) functionality, I am utilizing it to compile the React component provided below. const Component = (): JSX.Element => { const categories = ['Fruit', 'Vegetables']; return ( <ul> ...

Issue with Angular 5 EventEmitter causing child to parent component emission to result in undefined output

I've been trying to pass a string from a child component to its parent component. Child Component: //imports... @Component({ selector: 'child', templateUrl: './child.component.html', styleUrls: ['./child.c ...

What steps do I need to take in order to implement a functional pagination menu in Vue?

I downloaded and installed laravel-vue-pagination with the following command: npm install laravel-vue-pagination After that, I globally registered it in my app.js file: Vue.component('pagination', require('laravel-vue-pagination')); F ...

Matching TypeScript against the resulting type of a string literal template

My type declaration looks like this: type To_String<N extends number> = `${N}` I have created a Type that maps the resulting string number as follows: type Remap<Number> = Number extends '0' ? 'is zero' : Number ...

"Utilizing Vue-router to dynamically update meta tags based on route properties

I'm facing an issue with changing the meta attribute in my .vue file. Currently, I am only passing the id property, but I want the name to be set using meta.name. I attempted to implement the solution suggested by Phil (here), but unfortunately, it di ...

vuejs props become null upon page refresh

MyComponent.vue template: <ResponsiveNavigation :nav-links="navLinks" /> script data: () => ({ navLinks: [] }), created: function() { this.getSocialMediaLinks(); }, methods: { getSocialMediaLinks() { var self = this; ...

Problem: Unable to locate the TypeScript declaration file

I am facing an issue with my TypeScript configuration. I have two files in /src/models/: User.ts and User.d.ts. In User.ts, I am building a class and trying to use an interface declaration for an object defined in User.d.ts. However, User.ts is unable to a ...

Is there a way to dynamically adjust the size of mat dialog content to match the size of the mat dialog in

I have adjusted the size of my mat dialog, but the content is still stuck at the original size. To illustrate, here are some images: https://i.stack.imgur.com/2lLV2.jpg After researching, I found that it can be resized using CSS. I attempted the followin ...