The value property or method is not defined on the Vue.js 2 nested component instance

I created a simple Vue.js 2 example to test nested components.

Here is the structure of the components and templates:

    Vue.component('form-component', {
        template: '#form',
        props: ['value'],
        methods: {
            onInput: function (event) {
                this.$emit('input', event.target.value);
            }
        }
    });

    Vue.component('message-component', {
        template: '#message',
        data: function () {
            return {
                msg: 'Hello'
            }
        },
        props: ['user']
    });

    Vue.component('welcome-component', {
        template: '#welcome',
        data: function () {
            return {
                user: 'ahmad'
            }
        }
    });

    new Vue({
        el: '#container'
    })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>

<!--Form Template-->
<template id="form">
    <div>
        <div class="form-control">
            <label>Enter Your Name:</label>
            <input type="text" v-bind:value="value" :input="onInput">
        </div>
    </div>
</template>

<!--Hello Message Template-->
<template id="message">
    <div>
        <h3>{{msg}} {{user}}</h3>
    </div>
</template>

<template id="welcome">
    <div>
        <form-component :value="value"></form-component>
        <br>
        <message-component :user="user"></message-component>
    </div>
</template>

<div id="container">
    <welcome-component></welcome-component>
</div>

However, when running the app in the browser, the following error message is displayed:

[Vue warn]: Property or method "value" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

found in

---> <WelcomeComponent>
       <Root>

What could be the issue?

Update:

I have recreated this Fiddle based on a chapter from Learning Vue.js 2. I made some changes to parameter names, component names, and template names. Interestingly, when I transferred the main fiddle to my code, everything worked as expected.

Answer №1

Within your form-com component, you have the ability to establish a v-model to connect the input value and create a watcher to monitor changes in the input. This watcher then triggers a custom event that notifies the parent component of any modifications.

Vue.component('form-com', {
        template: '#form',
        data(){
            return{
                myInput:''
            }
        },
        watch: {
            myInput: function (inputVal) {
                this.$emit('input', inputVal);
            }
        }
    });

    Vue.component('message-com', {
        template: '#message',
        data: function () {
            return {
                msg: 'Hello'
            }
        },
        props: ['user']
    });

    Vue.component('welcome-com', {
        template: '#welcome',
        data: function () {
            return {
                user: 'ahmad'
            }
        },
        methods:{
            updateUser(value){
                this.user = value;
            }
        }
    });

    new Vue({
        el: '#container'
    })

To capture the events emitted by the child **form-com** component, you can use v-on:input or the shorthand @input directly within the parent template (welcome component) where the child component is included.

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>

<!--Form Template-->
<template id="form">
    <div>
        <div class="form-control">
            <label>Enter Your Name:</label>
            <input type="text" v-model="myInput">
        </div>
    </div>
</template>

<!--Hello Message Template-->
<template id="message">
    <div>
        <h3>{{msg}} {{user}}</h3>
    </div>
</template>

<template id="welcome">
    <div>
        <form-com @input="updateUser($event)" ></form-com>
        <br>
        <message-com :user="user"></message-com>
    </div>
</template>

<div id="container">
    <welcome-com></welcome-com>
</div> 

Check out the jsFiddle for a visual representation.

If using a watcher doesn't suit your needs, consider utilizing a computed setter. Take a look at the alternative fiddle showcasing this approach.

Answer №2

The Component 'welcome-com' is incomplete because it is lacking the value object:

Vue.component('welcome-com', {
        template: '#welcome',
        data: function () {
            return {
                value: '',
                user: 'ahmad'
            }
        }
    });

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 act of invoking a method does not alter the value of a DOM property" - excerpt from the testing-vue-js-applications

I recently picked up a new book on Vue.js testing titled Testing Vue js applications Here's a snippet of code from the book: <template> <div :class="{ hidden: isHidden}" :style="{ width: '0%', }&q ...

The collapsible tree nodes overlap one another in the D3.js visualization

I'm currently working on integrating a d3 code into Power BI for creating a collapsible tree structure. Each node in the tree is represented by a rectangular box, but I've run into an issue where nodes overlap when their size is large. Below is t ...

Changing the filepath for the build script in the package.json file for a NextJS project when deploying to Heroku

My project is currently structured as Root: Folder 1/ Folder 2/ Folder 3/ .. Frontend/ The front end folder contains my Nextjs project, package.json file, and everything else. However, Heroku requires the content in the Frontend/ folder to be in the root ...

What is the best way to transfer values or fields between pages in ReactJS?

Is there a way to pass the checkbox value to the checkout.js page? The issue I'm facing is on the PaymentForm page, where my attempts are not yielding the desired results. Essentially, I aim to utilize the PaymentForm fields in the checkout.js page as ...

Retrieve all documents with a matching objectId field in MongoDB

I built an API that can fetch all data from a table, but within this table there is an object ID reference to a user. Table 1 - Story | Table 2 - User api.get('/all_stories', function(req, res) { Story.find({}, function(err, stories) { ...

Need assistance as require function is not functioning as anticipated

const THREE = require('three'); require('three/examples/js/loaders/OBJLoader.js'); Once I imported threejs from node_modules, I decided to utilize the provided OBJLoader, but encountered an unexpected error. THREE is not defined a ...

Talebook: Unable to modify UI theming color

As I embark on creating my own theme in Storybook, I am closely following the guidelines outlined here: Currently, I have copied the necessary files from the website and everything seems to be working fine. However, I am facing an issue when trying to cus ...

Access-Control-Allow-Origin does not permit AngularJS Origin http://localhost:8080

I'm working on a web application using AngularJS. It's an admin interface that depends on a json-rpc API hosted on a different domain. When testing in my local environment, I encountered the error "Origin http://localhost:8080 is not allowed by ...

Show multiple items using v-for in a single textarea

Looking for a solution to display looped values within a single textarea instead of separate textareas. Currently using Laravel and Vue, which is showing the values individually in their own textarea elements. <textarea> <div v-for="(ite ...

Utilize Vue Slot with variable names for dynamic integration

My current dilemma involves a unique component called TableContainer, featuring dynamically named slots. // ... <td class="..."> <slot :name="'statuses-' + item.id" /> </td> <td class=""> ...

"Encountering issues with getJson function when used on a web hosting

Issue with Web Service JSON Connection: http://mohamedbadr.com/webservice/list.php File Fetching Results: http://contestlancer.com/web/getList.php Code for Getlist file: <!DOCTYPE HTML> <html> <head> <title>Hotel Promotion ...

Is there a way to access a computed property within methods?

Currently, I am utilizing this particular package to implement infinite scrolling in Vue. In order to continuously add new elements during each scroll, I fetch JSON data from my API server and store it in a data object. Subsequently, I divide the array in ...

Leveraging Node.js to establish a connection between two pug files

Recently, I decided to delve into the world of pug and JavaScript. However, I seem to be facing a small issue that I can't quite figure out on my own. My project involves creating multiple .pug files with various text content that I can navigate betwe ...

How can I add .htaccess to Vue production when using npm run build?

While using Vue CLI 3 and vue-router with history mode, I encountered this particular issue. Upon further investigation, I discovered that inserting a .htaccess file inside the dist folder after running npm run build resolved the issue. Is there a way to ...

What is the best way to navigate to an element on a webpage?

I am currently experiencing an issue with my chat widget where it displays an array of messages when I scroll up. The problem I am facing is that the slider remains fixed at the top when new messages load. I would like it to automatically focus on the la ...

The elegant-admin template's mobile navigation toggle is missing

I recently downloaded an admin theme and added the CSS to my Django static files. However, after doing so, the mobile toggle feature disappeared. I double-checked all the CSS and JS links in the index template, and they are correctly linked to the paths, b ...

Why does 1&&2&&3 result in 3? Could someone clarify this for me?

During a recent interview, I was presented with this question about JavaScript evaluation order. My understanding is that in JavaScript, evaluation proceeds from left to right. So would 1 && 2 result in false? I came across another discussion where it w ...

DiscordjsError: The client attempted to use a token that was not accessible

Hey there, I'm currently working on implementing a bot for my server and encountered an issue while attempting to create a member counter for voice channels. After completing the setup, I ran node index.js in the terminal, only to receive an error ind ...

Improving List performance with React.cloneElement

I am uncertain about the usage of React.cloneElement within a List component. Is it recommended to avoid using it, especially when dealing with a large number of elements in the list? Does React.cloneElement cause unnecessary re-renders that can be optimal ...

Is it possible to detect inline elements that have been wrapped?

I am facing a challenge with displaying an indefinite amount of in-line elements. Depending on the width of the browser, some elements may shift to a new line. I am curious to know if it is possible to identify and isolate these rows of elements, or if the ...