Error Encountered: Unresolved ReferenceError in VUE 3 Component while updating modelValue

I am encountering the error "Uncaught ReferenceError: comp1 is not defined" when emitting "update:modelValue" from the component.

Interestingly, this issue only occurs after I have built the project, not during development.

Parent:

<script setup>
import TestComp from "/src/components/TestComp.vue";
</script>
<script>
export default {
  data() {
    return {
      comp1: null // If I remove this line, the error will stop, but the watcher won't function properly
    };
  },
  components: {
    TestComp
  },
  watch: {
    comp1(newVal, oldVal) { console.log(oldVal, newVal) },
  }
};
</script>

<template>
  <TestComp
    v-model="comp1"
    class="form-control"
  />
</template>

Component:

<script>
import { defineComponent } from "vue";

export default defineComponent({
  data() {
    return { input: '' }
  }
});
</script>

<template>
    <input
      type="text"
      v-model="input"
      @change="$emit('update:modelValue', input)"
    />
</template>

Answer №1

To fix the issue, simply reposition the TestComp import to the second <script></script> block and remove the initial

<script setup></script>
.

Ensure that your Parent page code looks like this:

<script>
import TestComp from '/src/components/TestComp.vue'

export default {
  data() {
    return {
      comp1: null, // Removing this line may resolve the error, but the watcher will not function correctly
    }
  },
  components: {
    TestComp,
  },
  watch: {
    comp1(n, o) {
      console.log(o, n)
    },
  },
}
</script>

<template>
  <TestComp v-model="comp1" class="form-control" />
</template>

Answer №2

When working with a child component, it's important to utilize the value attribute and @input event to replicate the functionality of the v-model directive. In addition, don't forget to define the modelValue prop within the component:

<script>
import { defineComponent } from "vue";

export default defineComponent({
   props:{
    modelValue:String
  }
});
</script>
<template>
    <input
      type="text"
      :value="modelValue"
      @input="$emit('update:modelValue', input)"
    />
</template>

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

Guidelines for creating numerous self-edges within a node-link diagram using D3

Creating a single self-link on a node within a node-link diagram can be accomplished by following the instructions outlined in this resource: D3 Force Layout Self-Linking Node If you need to draw multiple links on the same node, what modifications would y ...

When using AngularJS filter, the comparator will evaluate to true and display the ng-repeat list even when the input

Recently, I stumbled upon this interesting example fiddle showcasing the use of a comparator parameter to filter exact matches: http://jsfiddle.net/api/post/library/pure/ The priority is supposed to be a number between 1-100, but due to inputting it as t ...

Combining file and JSON data in React and Express: A guide to uploading both simultaneously

I am looking to send both a file and JSON data from a form using my react handle submission method. const formData = new FormData(); formData.append('File', selectedFile); const data = { name: 'vasu', email: 'example@example ...

Delving into the World of ES6 Symbols

Throughout my experience with various programming languages, from C# to Lisp to Scala to Haskell, symbols have consistently behaved as singleton objects. This means that any two symbols with the same name are guaranteed to be identical. In Racket: (equal? ...

Error message for Laravel Passport authentication failure

I've been stuck on this issue for a while now and can't seem to pinpoint the problem. I am using Laravel on the backend and Vue on the front end. Logging in works fine, I receive the token, but when I try to access a route with auth:api, I get an ...

Sort through the JSON data and showcase it in a gridded format

I need assistance with displaying data from a JSON object in a grid based on user selections. The user should be able to select a year and make from a dropdown menu, and the corresponding data should then be filtered and displayed in a grid. For example, i ...

Is it possible to store an HTML element tag in a variable?

I've been learning JavaScript on w3schools and freecodecamp, but I stumbled upon something in w3schools that has me puzzled. Can someone please explain why this particular code snippet works? The variable "text" is declared as containing the string " ...

Page encountering NextJS cors error, while API route remains unaffected

In my Next.js application, I have encountered an issue where executing a call to fetch a page from an external website works perfectly fine when done from the backend through an API route. However, when attempting the same action from the frontend, I encou ...

What is the reason for the num pad being classified as a character?

Everything is functioning correctly, but when I use the number pad on the right side of my keyboard, it registers as a character and gets deleted. However, the numbers on the left side are accepted without any issue. I want to be able to input numbers usin ...

What could be causing the redirection to my php file in this particular contact form?

I have a contact form on my website that uses AJAX for submission. Everything seems to be working fine, but when the user clicks on the Submit button, they are redirected to the PHP file instead of staying on the page to see success or error messages. I wa ...

Experimenting with API request in Vue using Moxios

I am facing an issue with testing an API call that occurs in the "mounted" lifecycle hook. In my single file component, I am responsible for presenting information about an "Owner". The functionality works as expected in the browser. <template> & ...

Tips on using the html() and toContain() functions in shallowMount in Vue to test a div that includes another component

I am encountering an issue with shallowMount in Vue. Here is my function: describe('ParentComponent.vue', () => { it('renders a ParentComponent', () => { const wrapper = shallowMount(ParentComponent, { propsData: { ...

Steps for importing a jQuery script into a Vue component

Looking to include a basic jQuery script in my Vue file. Attempted to import it using the following method: In the Vue file import {myScript} from "@/components/scripts/myScript.js" Then in the myScript.js file : export const myScript = { $('.cl ...

Validating forms in express.js

I need to validate a form that includes user details. In addition to the basic validation for checking if fields are not empty, I also want to verify if the username/email exists in the database. For the email field, I need to ensure it is not empty, follo ...

Vue - Struggling to invoke sweetalert2 within a method

I'm encountering an issue with launching sweetalert within a Vue method. When I directly call the sweetalert in the script, it works fine. However, when I try to launch the sweetalert upon clicking a button, nothing happens (no errors in the console). ...

The protection ensured by employing an extensive hash in the query string for the recognition of a changing document

I am currently developing a small web application that creates exercise program printouts. The concept is simple - a user like a personal trainer can craft an exercise regimen and send it to their client via email using a unique link. http://www.myurl ...

Is there a way to deactivate a button upon clicking and then substitute it with a new button that serves a different purpose in AngularJS?

Is there a way to deactivate a button once clicked and substitute it with another button that performs a different task using AngularJS? Here is the button in question: <button type="submit" class="btn btn-small btn-default" ng-disabled="isteam==0 || ...

Can you explain the significance of triple brackets (e.g. {{{ content }}}) in the context of Javascript/Typescript?

As I delve into the code of a fresh project in which I hope to contribute, I have come across numerous methods defined with triple brackets, like so: deinitialize() {{{ this.destroyed = true; $(window).off("resize", this.resize as () => void); ...

The automated test locator in Angular using Protractor is failing to function

I am facing a challenge with my angular web application as there are some elements that are difficult to interact with. One specific element is a checkbox that needs to be checked during testing: ` <div class="row form-group approval_label"> < ...

An unexpected error occurred during page generation. Any relevant console logs will be shown in the browser's terminal window

Encountered a RangeError: Maximum call stack size exceeded while compiling this web app in the browser. The error occurred on my header tag and sometimes on the return tag. What should I do? export default function Header() { return ( <> ...