Vue3 data injection failure occurs when attempting to pass data to sibling and parent components within the

Greetings fellow enthusiasts of the modern lightweight web! I find myself in need of assistance with vue3.

I am working on sharing timetable details between multiple child components and showcasing the top five and worst five time wasters as an example. One of the components is supposed to add new time data to the existing components using an @click="$emit" functionality. However, I'm facing an issue where the added "Lux" data appears for a brief moment and then reverts back to the default data without affecting the components.

Therefore, I humbly request one of you skilled individuals to point out the mistake I may have made.

App.vue

<template>
   <Top5 :projects="entries"/>
   <TimeDetail @add-time-data="newTimeData"/>
</template>

export default {
  name: 'App',
  methods: {
    newTimeData(details) {
      this.entries.push(details);
      console.log(details);
    } 
  },
  data() {
      return {
      entries: [
          {client:'Test', time: 20, date: '2020-09-03'},
          {client:'Test2', time: 30, date: '2020-09-04'},
          {client:'Test3', time: 45, date: '2020-09-05'}
         ]
      }
  },

TimeDetail.vue

<template>
    <div class="container-fluid">
        <form>
            <div class="row">
                <div class="col-md-1">
                    <label for="workdate">Date</label>
                    <input type="date" class="form-control" id="workdate">
                </div>
                <div class="col-md-2">
                    <label for="client">Client</label>
                    <input type="text" class="form-control" id="client">
                </div>
                <div class="col">
                    <label for="workhours">Hours (0.5 - 8)</label>
                    <input type="range" class="form-control-range" id="workhours" min="0.5" max="8" step="0.5" value="0.5" oninput="this.nextElementSibling.value = this.value">
                    <output>0.5</output> hours
                </div>
                <div class="col-md-1">
                <br><button type="submit" class="btn btn-primary" @click="$emit('add-time-data',{client:'Lux', time: 66, date: '2020-06-06'})">Submit</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
export default {
  name: 'TimeDetail',
  emits: ['add-time-data'],
}
</script>

PS: Unfortunately, Console and vue.js devtools are not functioning properly due to vue.js not being detected error and console failing to display logs :(

Answer №1

To prevent the default behavior of a form submitting when a click event is fired by a button with type submit, make sure to include the prevent modifier:

<button type="submit" ... @click.prevent="$emit('add-time-data',...)">Submit</button>

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

Utilize the request object from Express within a Mongoose Plugin

I have incorporated an API in ExpressJS along with a middleware that runs before each endpoint controller: app.use(segregationMiddleware); app.get('/some-endpoint', controller1); app.get('/some-endpoint-2', controller2); The segregat ...

How can Navbar values in a React application be dynamically updated depending on the user's login status?

As someone new to React, I am exploring the correct approach to building a Navbar that dynamically displays different links based on a user's login status. When a user logs in, a cookie loggedIn=true is set. In my Navbar component, I utilize window.se ...

Exploring the possibilities of utilizing React server components in my project

I am interested in experimenting with the new React API for making server-side component calls. However, I am unable to find any information on how to begin a project using server components. In an example of source code that I stumbled upon, it mentioned ...

Filter an array of objects based on a provided array

I have an array of objects that I need to filter based on their statuses. const data = [ { id:1, name:"data1", status: { open:1, closed:1, hold:0, block:1 } }, { id:2, name:"data2", ...

Tips for building an API using an app router in Next.js

After setting up my new project and using the app router as recommended in the latest version of Next.js, I am now looking to create an API. How can I go about creating and utilizing this API within my page app/user/page.tsx? I have already created an API ...

Tips for modifying the color or personalizing the header arrow for the expandableRow within Mui Datatable

My MUI data table has the expandable rows option, along with an ExpandAll button in the header row. The arrow displayed in the title row is confusing for users as it's similar to the arrows in all other rows. I want to change the color of the header a ...

What causes the JavaScript program to fail to execute?

This is my JavaScript program that is not displaying anything when I press search. The function button-pressed() works perfectly on its own, but when I try to use the code within a form to input text, it doesn't work. However, when I try to print the ...

Hiding jQuery Mobile Anchor Button: A Step-by-Step Guide

My goal is to dynamically hide or show specific anchor buttons based on certain conditions, but I am currently facing issues with this functionality. Within my jQuery Mobile setup, I have multiple anchor buttons that follow a similar structure: <a id= ...

I am struggling to get the pop-up to close using the current code. I suspect that the issue might be related to the variable I was previously using in WordPress. I have made changes but the pop-up

While delving deeper into the realm of Javascript, I encountered a stumbling block with a single popup intended for the main page of a WordPress website I am constructing. Despite my attempts to modify the code's variables, they seem unyielding. Surpr ...

Creating a formatted JSON string from the data retrieved using a GET request and embedding it into an HTML template to be sent as the

Struggling to send JSON data retrieved from a GET METHOD as an email. The challenge is defining the body of the email using the obtained JSON object. Looking for solutions! Below is a snippet of my code: app.get('/userinfo',(req,res)=>{ ...

Is there a way to determine the items that will be displayed on this page by clicking a link from a different page?

First Page: Whenever a link is clicked, it will redirect to the second page where only the relevant item will be displayed <ul class="portfolio-filters list-inline"> <li ><a href="page2.html/#webdesign-filter">Web ...

The content in the div tag isn't showing up properly because of Ajax

I am facing an issue with my Ajax query. Even though I can retrieve the results by posting, they are not displaying in the designated div tag on mainInstructor2.php. Instead, the results are showing up on a different page - specifically, on InstructorStude ...

Can anyone guide me on creating a Mapbox map layer using React.js?

I'm currently in the process of creating a polygon layer on top of my Mapbox map using ReactMapboxGL. I have some Mapbox Javascript code that I need to convert into React.js format: map.on('load', function () { map.addLayer({ ...

Unraveling the Mysteries of AngularJS in a Tutorial Snippet

After reading through the enlightening theory snippets in Step 3 of AngularJS Tutorial, one particular passage piqued my curiosity: The scope, which connects our controller and template to create a dynamic view, is not isolated within its own bounda ...

Troubleshooting the issue with loading dynamic image src in Vue.js

I recently delved into the world of Vue.js and Vue CLI, but I've run into an issue. I'm puzzled as to why I'm unable to dynamically set the image from the scope, even though it works when directly written in the HTML. The obj.img is a string ...

The modal is not displayed when the on click listener is triggered

I'm a beginner in the world of programming and I'm currently working on creating modals that pop up when clicked. However, I'm facing an issue where the pop-up message doesn't appear when I click the button. Oddly enough, only the overl ...

Updating React props using useState?

Below is a component that aims to enable users to update the opening times of a store. The original opening times are passed as a prop, and state is created using these props for initial state. The goal is to use the new state for submitting changes, while ...

How can I pass server-side variables to a Vue instance using webpack and vue-cli?

I am currently working on a project with vue-cli, which creates a Node project with webpack. I can successfully build all scripts and also utilize the pug language within .vue files. However, the project uses HTML-Webpack-Plugin during the build process, w ...

Exploring asynchronous data handling in AngularJS using promises

Currently, I am working on a single page application using angularJS and encountering some difficulties in storing asynchronous data. In simple terms, I have a service that contains my data models which are returned as promises (as they can be updated asy ...

Issue encountered while executing a freshly created VUE JS Hello.world application

My journey into app development begins with creating my first app using CLI: vue create hello-world. Upon attempting to launch the app using npm run serve in the CLI, it runs successfully. However, when I try to start it with npm run dev, an error is throw ...