The @model decorator in vue-property-decorator is known to trigger a warning in Vue: It is advised to

I'm currently using TypeScript in conjunction with this library. In my file HomePage.vue, I have the following code:

<template>
    <div>Enter text here: <input v-model="someName" type="text"></div>
</template>

<script lang="ts">
import { Component, Model, Vue } from "vue-property-decorator";

@Component
export default class HomePage extends Vue {
    @Model() someName: string;
}
</script>

Upon compilation and running in the browser, when I enter something in the input box, I encounter the following Vue warning in Chrome console:

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "someName"

Does anyone have a solution to tackle this issue?

Answer №1

Summary:

Transitioning from React to Vue, I found the previous answer to be somewhat helpful but lacking in addressing the specific query regarding how to utilize the @Model decorator. While Provide and Inject can work for passing props between parent and child components, it may be excessive in certain scenarios. The documentation on this topic seemed ambiguous, leading me to spend a considerable amount of time pondering over it. Nonetheless, it's important to note that when dealing with packages referring to props, annotations like @Prop, @PropSync, and @Model are typically applied at the child component level. Here is my approach to resolving the issue without encountering any unpleasant console errors. Starting with the parent component:

<template>
  <div>
     <input type="text" v-model="modelText" />
    <Child 
      :modelText="modelText"
    />
  </div>
</template>

<script lang="ts">
import Component from 'vue-class-component';
import Vue from 'vue';
import Child from './Child.vue';

@Component({
  components: {
    Child,
  }
})
export default class Parent extends Vue {
  private modelText: string = 'model-text';
}
</script>

Moving on to the child component:

<template>
    <div>
      @Model: {{modelText}}
    </div>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Prop, PropSync, Model } from 'vue-property-decorator';

@Component({

})
export default class Child extends Vue {
  @Model('input', { type: String }) modelText!: string;
}
</script>

Answer №2

Starting from version 9.1.2, the view-property-decorator now includes support for the @VModel decorator.

Here is an example:

import { Vue, Component, VModel } from 'vue-property-decorator'

@Component
export default class CustomComponent extends Vue {
  @VModel() name!: string
}

This setup enables two-way data binding with the name property within the component, and allows the use of v-model="..." externally without any pesky warnings!

https://github.com/kaorun343/vue-property-decorator#-vmodelpropsargs-propoptions-decorator

Answer №3

When using v-model, it is important to utilize properties that are explicitly defined within the data section.

Instead of the shorthand @Model, consider using @Provide in order to properly define the necessary data properties.

@Provide() foo = 'foo'

For more information, visit https://github.com/kaorun343/vue-property-decorator

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

In Angular 2, I am having trouble reaching the properties of an object nested inside another object

I have a variable named contact. When I used console.log(contact) to display its contents, this is what I got: addresss:[] company:"" emails:[] id:3 internet_calls:[] lat:"10.115730000000001" lng:"76.461445" name:"Diji " phones:[] special_days:[] timesta ...

How can I run a Redis Server on a port other than the default 6379 port using ioredis?

I recently came across this query regarding starting redis-server on a different port than the default 6379 in Ubuntu. I am currently trying to achieve the same using ioredis in my NestJS project, but unfortunately, it refuses to connect to any other port ...

Troubleshooting issue with getServerSideProps not functioning in Next.js while utilizing Next-redux-wrapper and TypeScript

When attempting to trigger an action as outlined in the documentation using the getServerSideProps function with the help of next-redux-wrapper store and redux-thunk, I am encountering the following TypeScript error: ts(2322): Type '({ req }: GetServe ...

Challenges when transitioning to TypeScript within a React project

I'm in the process of converting my React app Components to Typescript. After installing the TS package and setting up a tsconfig.json file and react-app-env.d.ts files, I made sure to change the file extensions from .js to .tsx. However, I encounter ...

Guide to including spinner in React JS with TypeScript

I need help with adding a spinner to a React component. The issue I'm facing is that the spinner does not disappear after fetching data from an API. Can someone please point out what I am doing wrong? Here is the code snippet: import React, { useSta ...

Steps to create an Executable from a complete Typescript project

My current project in Typescript consists of multiple folders and config files that are interconnected to form a larger script. I am looking for the best approach to compile it into an executable. Key points to consider: • Multiple Typescript files w ...

Improving the management of user input in Lit components

I am seeking a more efficient method to manage all inputs within my lit component while minimizing the code usage. I am also employing Typescript in this process, utilizing @change=${this.handleInput} for all input fields. Below is an example of how the ha ...

Leveraging Jackson annotations in a derived class

In the process of developing my Android App, I encountered a challenge with deserializing JSON using the Jackson Annotation API. Everything was running smoothly until I attempted to integrate the AndroidActive ORM, which required your POJO to inherit from ...

Updating a value in one input field triggers changes in several other input fields in Angular 6

I'm currently in the process of learning the basics of Angular 6 and TypeScript, and I'm struggling to figure out how to achieve a specific functionality. Essentially, I have a field where users can input a numerical value, and based on that inpu ...

Best practices and distinctions when it comes to typing in TypeScript functions

Do the typings below differ in any way, or are they essentially the same with personal preference? interface ThingA{ myFunc(): number; } interface ThingB{ myFunc: () => number; } ...

What is the best way to finalize the pre-departure confirmation for react components, covering both class components and function components?

After much searching, I have yet to find the answer to this question. It seems like the solution could lie in the Prompt component from react-router v4. However, since I am using version 6.x of it, I'm left wondering what the alternative is for the Pr ...

Utilizing the <HTMLSelectElement> in a Typescript project

What exactly does the <HTMLSelectElement> do in relation to a TypeScript task? let element = <HTMLSelectElement> document.querySelector('#id_name'); The HTMLSelectElement interface, similar to the one mentioned in TypeScript, is exp ...

How to Close Keyboard on iOS and Android using Angular 4 TypeScript

Currently, I am developing an application using angular 4 alongside typescript. The main issue I am encountering involves the keyboard on mobile devices when displaying a div at the bottom. Since the div is positioned at the bottom, it gets obstructed by ...

Items in the Vue.Draggable display can be arranged on both the left and right

Recently delving into Vue.js, I am utilizing Vuedraggable for item dragging in my project. Currently, the items in the draggable div are shown as Text 1 Text 2 Text 3 Text 4 Is there a way to rearrange them to display like this? Text 1 Text 2 Text 3 T ...

Passing parameters in React classes is a crucial aspect of creating

Within a project I am currently working on, there is a const defined for the page header which takes in parameters and utilizes the information: interface UserProps { user?: { name: string, image: string }, loading?: boolean, error?: E ...

Trouble arises when trying to navigate to a new page from ion-tabs within Ionic 2

I recently developed a chat application that redirects users to a tabs page upon login. <ion-tabs tabsPlacement="top" color="header" tabsHighlight=true> <ion-tab [root]="tab1" tabTitle="Chats" tabIcon="chatbubbles"></ion-tab> <io ...

Generate a dynamic interface using properties and options provided in a JavaScript object

Can Typescript support the following scenario: I have a structure where keys represent properties and values are arrays of options for those properties: const options = { foo: [fooOption1, fooOption2, ...], bar: [barOption1, barOption2, ...], ... } ...

What is the best way to implement line-through text decoration for a list item when it is clicked in an Angular application? Additionally, how can the "completed" value in

<h1>My Exciting To Do List</h1> <input #newTask> <button (click)="addTask(newTask.value)">Add Task</button> <ul> <li *ngFor="let task of tasks">{{task.name}}</li> </ul> export cl ...

Can you tell me the data type of a Babel plugin parameter specified in TypeScript?

Struggling to find ample examples or documentation on writing a Babel plugin in TypeScript. Currently, I am working on a visitor plugin with the following signature: export default function myPlugin({ types: t }: typeof babel): PluginObj { In order to obt ...

Exploring object traversal in Typescript

Why is the output undefined? I even tried giving timeObject a type of any, but it still doesn't produce the expected result. const timeObject = { 'On web close': 0, '10 secconds': 10000, '8 hours': 28800000, ...