Switching between components in Vue.js

I am a beginner with vue.js and I have encountered a challenge. On my page, I display a list of people with an 'Edit' button next to each person's details. My goal is to switch to another page when the Edit button is clicked, where I can edit the selected person's information.

Currently, I am using Bootstrap and Router in my solution but I'm unsure about the best approach to achieve this. I am considering two options:

Option 1: One idea is to route to '/person/:id' upon clicking the Edit button, but I am struggling with implementing this functionality within the click handler method.

Option 2: In the main component below, named Home.vue, I am attempting to toggle between two components - 'PersonsList' and 'EditPersonData' based on events emitted by PersonsList component. Although PersonsList successfully emits the event, I am uncertain about how to listen for it and switch to the EditPesonData component.

Any assistance you could offer would be greatly appreciated.

Thank you

Answer №1

To efficiently retrieve user details, it is recommended to pass the user's identifier as a parameter in the route URL, such as /person/:id. To implement this, you can dynamically append the id of each user item like this:

<div v-for="(user,index) in userList" :key="index">
     <a :href="'/person/' + user.id">Edit</a>
</div>

Then, within another component, access this id value using this.$route.params.id to fetch additional information related to that particular user.

Ensure that the route in your router configuration includes the id parameter, structured as: person/:id

UPDATE :

If there is a need to modify the route upon a button click event:

<b-btn @click="editPerson(user.id)">Edit</b-btn>

Add the following method in your script section to handle the edit function:

editPerson(userId){
     this.$router.push({
          name: 'person', 
          params: { id: userId }
     });
}

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 functionality of window.location.href seems to be malfunctioning on mobile devices within an ionic application

Attempting to open a view with a function call from the UI side <ion-option-button class="button-light icon ion-chatbubble" ng-click="openView('username')"></ion-option-button> The controller code for this action is as follows: $sc ...

Is it possible to utilize a single Promise multiple times?

// App.js sites[site_name].search(value).then(function(results) { console.log(results); }); // SearchClass.js Search.prototype.search = function(search) { var self = this; this.params['wa'] = search; return new Promise(function ...

Utilize JSON parsing with a reviver parameter to filter out specific object properties

In the process of developing a Node.js server, I am working on a particular service that requires accepting a stringified JSON object while also implementing field whitelisting. To achieve both of these objectives, I intend to utilize JSON.parse() with the ...

What is the method for activating the on collapse event with a bootstrap navbar?

I am encountering a common issue with collapsing the navbar on smaller screens and triggering an event when the collapse button icon is clicked. Despite my efforts to find a solution, I have been unsuccessful in using the following JavaScript code: $(&apos ...

AngularJS - Unchecked radio button issue

Within my code, there is an ng-repeat including several radio buttons: <div class="panel panel-default" ng-repeat="item in vm.itemList"> ... <td ng-show="item.edit"> <div class="row"> ...

Swapping React Components with a Click of a Button

My webpage features a button labeled "Sign Up". Once this button is clicked, I want it to display a new component named "SignUp" in place of the original button. Currently, my method involves using setState to trigger the rendering of the new component upo ...

Restrict the height of posts created in the summernote editor

My goal is to create a structured page application using summernote. When using summernote, a div with contenteditable=true is appended to the body to allow users to add content. However, I want to set a fixed height for this div so that users can only ent ...

There appears to be an issue where the session object cannot be retrieved by the Struts2 action

I have a struts2 action that is invoked by a JavaScript function. The JavaScript function uses uploadify to enable multiple file uploads: <script type="text/javascript"> $(document).ready(function() { $("#fileupload").uploadify({ ...

Extract the Date portion from a DateTime object in ASP.NET MVC

I am currently facing an issue with a property in my Model [Display(Name = "День рождения")] [DataType(DataType.Date)] public System.DateTime Birthday { get; set; } When trying to save the value to the database using AJAX, it is also ...

Guide to automatically closing the calendar once a date has been chosen using owl-date-time

Utilizing Angular Date Time Picker to invoke owl-date-time has been functioning flawlessly. However, one issue I have encountered is that the calendar does not automatically close after selecting a date. Instead, I am required to click outside of the cal ...

Experiencing the Pause: A Fresh Take on

I'm currently using this slideshow for a project, and I need to understand if it's possible to resume its interval. The current issue is that when you hover over the slideshow, the progress bar stops. But once you remove the mouse, it continues f ...

Likelihood of triggering a function based on percentage

Hey everyone, I'm looking to add some randomness to the buttons on my website. I have a Button that could activate function one, function two or function three. However, I want to make it so there is a 60% chance that function one gets called from the ...

JavaScript regex substitution not functioning as expected

My JavaScript code contains a string var str = '<at id="11:12345678">@robot</at> ping'; I am trying to remove a specific part of this string <at id="11:12345678">@ To achieve this, I am using the following code snippet: var ...

What is the best way to pass cookies between domain and subdomain using nookies?

Recently, I've been facing some challenges with sharing cookies between my app and website while using nookies. Below is the code snippet from my app.mydomain.com file: //setCookies to main domain setCookie(null, 'jwt', login ...

Presenting JSON data in a table format on-the-fly even without prior knowledge of the data's structure or field names

When working with my application, I will be receiving data in JSON format and I am looking to showcase this data in a tabular form within a web browser. It's important to mention that I won't know beforehand the structure or field names of the re ...

Arranging an Array of Arrays Containing Strings

Looking for a solution to sort an array containing arrays of strings? A similar issue was discussed here. Here is the array in question: var myArray = [ ['blala', 'alfred', '...'], ['jfkdj', ...

Allowing the contenteditable attribute to function only on a single line of

My app allows users to create different lists, with the ability to edit the name. However, I am facing an issue where if a user types a new name, it should remain on only one line. I tried adding max height and overflow hidden properties, but it only hides ...

Move the cursor to the bottom of a div that can be edited

I have been struggling to position the cursor at the end of the next or previous editable content tag if the current one is empty. However, when I try to set focus, it always ends up at the beginning of the text instead of the end. I've tried various ...

Implementing position sticky on a div depending on its content text - step by step guide

If the text inside the .newsDate matches the previous or next .newsDate, I want to make its position sticky when scrolling, until it reaches the next .newsMiddleCont div. What I'm trying to achieve: The same date on multiple news items should s ...

Obtain user information upon the first page visit using Nuxt middleware

I'm currently facing an issue with Firebase Auth integration in my Nuxt application, specifically concerning protected routes. The middleware I have set up in Nuxt handles redirection based on the user's login status during page navigation. The ...