Returning Props in Dynamic Components with Vue 3

Exploring the capabilities of Vue3's Dynamic Component <component>, I am currently working with this setup:

Component 1:

<template>
  <div>
    <h1> Name Input: </h2>
    <Input :model="props.name" />
    <p> {{props.example}} </p>
  </div>
</template>
<script>
export default{
  props:["props"]
}
</script>

Main File

<template>
  <div>
    <component :is='active.page' :props='active.props' />
  </div>
</template>
<script>
import UserName from 'components/UserName.vue'
export default{
  components: { UserName }
  setup() {
    const active = { page: 'UserName', props: { name: null, example: 'Dave' } }

    return {active}
  }
}  

In due time, there will be multiple pages for user navigation using a <Button />; my question is: Can the modified prop name be sent back to the Main File after changes are made in Component 1?

This is essential for passing it to subsequent pages within <component> for display when moving to the next screen.

I appreciate your assistance.

Answer №1

Utilize the emit method to send the name back to the Main File without directly changing the props value. Instead, create a new variable to store the name and make modifications to this new variable.

// Component 1
<template>
  <div>
    <h1> Input name: </h1>
    <Input v-model="name" />
    <p> {{props.example}} </p>
  </div>
</template>
<script>
import { defineEmits } from 'vue'
export default {
  props:["props"],
  emits: ['name-updated'],
  data() {
    return {
      name: this.props.name
    }
  },
  watch: {
    name() {
      this.$emit('name-updated', this.name)
    }
  }
}
</script>

// Main File
<template>
  <div>
    <component :is='active.page' :props='active.props' @name-updated="updateName" />
  </div>
</template>
<script>
import UserName from 'components/UserName.vue'
export default{
  components: { UserName },
  setup() {
    const active = { page: 'UserName', props: { name: null, example: 'Dave' } }
    const updateName = (newName) => {
      active.props.name = newName
    }
    return {active, updateName}
  }
}  
</script>

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

Guide to creating scroll-based animations for a Div element

I've been brainstorming ways to rotate a div as I scroll. My goal is to recreate the effect shown in this codepen. However, I'm struggling to implement scrolling functionality. What I envision is scrolling down causing the word Data to rotate in ...

Updating presence of HTML tag link based on ng-if condition

HTML: <a data-ng-if="price" data-ng-click="selected(price)"> <div> ... </div> </a> i am trying to figure out how to remove the <a></a> element when data-ng-if="!price" Can anyone provide guidance on th ...

Can anyone explain to me why the data I'm passing as props to the React functional component is displaying as undefined?

I have encountered an issue with a pre-made React component where I am unable to see the data being passed as props when I console log it. I am unsure if I am passing the prop correctly, as I have used the same prop successfully in other class-based comp ...

Trouble arises with the MUI 5 date picker when inputFormat is included

When I select a date from the calendar, everything works fine. However, if I set inputFormat="yyyy/MM/dd" and manually type in the date, it doesn't recognize the date format properly. Instead, it treats the input as a string like 11111111111 ...

When running the command "npx create-next-app@latest --ts," be aware that there are 3 high severity vulnerabilities present. One of the vulnerabilities is that "node-fetch

Just set up a fresh project with Next.js and TypeScript by following the documentation using npx create-next-app@latest --ts. Encountering multiple high severity vulnerabilities despite running npm audit fix --force, which actually adds more vulnerabiliti ...

Redux-form fails to retrieve the value of the SelectField

I'm trying to work with a simple form using react and redux-form. My goal is to gather all the field values from my form and send them to my RESTful API using jQuery ajax. Unfortunately, I've run into an issue where redux-form doesn't seem ...

Issue encountered while trying to download Jade through npm (npm install -g jade)

I am having trouble downloading jade via npm on my Mac (Yosemite). After downloading node and updating npm, I tried to install jade but encountered a series of errors that I cannot resolve. Even attempting to use sudo did not help, as it only displayed s ...

utilize vuejs to display an alternative route on a side panel

As I develop a back office using Quasar, I am exploring the possibility of creating what I would call a sidepage to open routes. This sidepage would display the requested component without any additional decoration, such as menus. However, there is also a ...

Leverage Vue.js with Vuex for state management to achieve two-way binding without the need for mutation via v-model

While I understand that mutations are typically used to change state, I couldn't help but wonder if it's technically feasible to utilize state in a v-model binding. This is how I currently handle it: In the HTML: ... <input v-model='to ...

Unable to configure unit tests for Vue project using Typescript due to TypeError: Unable to destructure property `polyfills` of 'undefined' or 'null'

I've been working on adding unit tests for an existing Vue project that uses Typescript. I followed the guidelines provided by vue-test-utils for using Typescript, but when I ran the test, I encountered an error message stating: TypeError: Cannot d ...

Stopping a jQuery AJAX request after receiving another response

I am facing a problem and I need some creative solutions :) Currently, I have two $.ajax calls in my code. The first call is asynchronous and takes a long time to respond (approximately 1 minute). On the other hand, the second call is synchronous (with as ...

How can we use the Selenium JavascriptExecutor in Java to return an Object from JavaScript?

I've encountered an issue while running a JavaScript file using Java with Selenium in my application. When I execute the JavaScript file with JavascriptExecutor after logging in, I'm only getting a null return instead of a valid one. Below is a ...

Issue with running the Jquery each function within a textbox inside an ASP.NET gridview

Below is the gridview markup: <asp:GridView ID="gvDoctorVisits" runat="server" DataKeyNames="AdmissionId" class="tableStyle" AutoGenerateColumns="False" Width="100%" EmptyDataText=& ...

Struggling with uploading a Vue project to Github Pages

I attempted to deploy my vue-cli project on Github Pages by following the guidelines provided on a website and on GitHub's support page. To deploy vue-cli to Github Pages, refer to this link: https://medium.com/@mwolfhoffman/deploying-to-github-pages ...

Transforming an array of strings into an array: a guide

An API call is returning an object with the following structure: data = { ... filter: "[1,2,3]" ... } I need to convert the string of array into an actual array of numbers, like [1,2,3]. Thank you! ...

Are you using yeoman with csslint in your project?

As a newcomer to grunt, I am facing challenges in integrating csslint to monitor my project while running "grunt serve" for csslinting. I am specifically struggling with disabling the adjoining classes warning since it is not relevant for IE6 compatibili ...

I was disappointed by the lackluster performance of the DataTable in CodeIgniter; it did not

I recently started using CodeIgniter and I'm having trouble getting the dataTable to work. Here's a snippet of my page: <table class="table table-striped table-bordered table-hover dataTables_default" id="dataTables-example"> ...

How come my customized directive is not taking precedence over the original methods in AngularJS?

Following the guidelines in the Angular Decorator manual (https://docs.angularjs.org/guide/decorators), I attempted to create a directive and apply decoration to it. The directive's purpose is to display the current date and time. I included a (point ...

In jQuery, conditionally nest divs within another div based on a specific requirement

There is a container with multiple nested elements that need to be rearranged based on the value of their custom attribute. The goal is to reorder those elements at the end of the container if their 'data-keep-down' attribute is set to true, usin ...

Changing the Date Display format of a new Date() instance

Just starting out with the MEAN Stack I'm currently utilizing the new date() function to retrieve the date, but I prefer not to display the time offset. ...