Are these 2 components very similar, with the only distinction being the unique GET request for each? Should we combine them into one?

There are currently two components in my project that are nearly identical. The HTML structure and CSS rules are the same, with the only difference being the GET request made in the mounted() lifecycle hook. One component fetches all visited places, while the other fetches all wishlisted places. Despite the different data being returned, the response structure is the same for both requests.

My dilemma is whether to merge these two components into one. If I decide to do so, how can I determine which type of data to fetch - visited or wishlisted places? One possible solution could be to base this decision on the URL path, such as checking for

http://localhost:8080/#/Admin/visited
to fetch visited places and
http://localhost:8080/#/Admin/wishlist
to fetch wishlisted places.

Furthermore, what would be an appropriate name for this combined component, considering it will be responsible for fetching both types of places? Additionally, what would be a suitable name for the data property that will replace wishlist and visited?

Answer №1

Break down the process of retrieving the information and presenting it. By doing this, the main component can handle the data fetching in its mounted/created hook and then pass it as a prop to the component responsible for displaying it:

<places :places="visited"/>
<places :places="wishlist"/>

Answer №2

It is recommended to create a reusable component, possibly named Places.vue. However, ensure that the structure of your returned result remains the same (consider renaming wishlistPlaces and visitedPlaces to places or sights):

The component should accept a source parameter, like this:

<places source="getWishlist" />

And define it in the component as:

props: {
  source: {
     type: String,
     required: true
  }
}

Your getter function should look something like this:

mounted() {
    axios.get(`/${this.source}/${this.userName}`)
    .then(response => {
        this.places = response.data.places
    }).catch((error) => console.log(error));
}

You will also need to update any instances where you are currently using wishlist or visited props/methods to instead use places (or sights if preferred).

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 Art of jQuery Data Processing

I'm currently working on extracting data from a form submission. Below is the code I've been using. function handleSubmission() { $("#questionForm").submit(function() { $.post("SubmitQuestion.php", $("#questionForm").serialize()).done(functi ...

Using JavaScript and jQuery, apply a class when a specific radio button is checked and the data attribute meets certain criteria

Need help with changing explanation color based on correct answer selection in multiple choice question. Currently, all choices turn green instead of just the selected correct one. Any assistance is welcome, thank you. html <li class='test-questi ...

What is the best way to modify the values in an initialized array once the fetchData method has been executed?

As a beginner in Vue.js, I am currently working on a project that involves updating initialized arrays in the data() section of my component using the fetchData method. Below is a simplified version of the code: <script> import axios from 'axi ...

One effective method for implementing two-way data binding with Vuex within a list

https://i.stack.imgur.com/xO2MO.png I have a collection of items stored in a Vuex state. The issue arises when I select items from the main state and update it using v-on:change, as well as from the sidebar. However, upon toggling down with v-on:change on ...

Utilize client-side script in nodejs to share module functionalities

I created a function within the user controller module to verify if a user is logged in on the site: exports.isLoggedIn = function(req, res, next) { if (req.user) { return true; } else { return false; } }; I'm unsure of h ...

Creating an AJAX request in Play 2.x by using a shared button

Although I have successfully utilized AJAX requests in the past using a form and adding return false to prevent page refresh, I recently encountered an issue when moving my JavaScript into a separate file. The @ commands now fail, making it difficult for m ...

I'm seeking an easy method to adjust the x and y coordinates of a popup rectangle with a fixed size. Can anyone provide

Is there a way to create a simple pop-up rectangle, possibly using jQuery or a similar tool, that presents a scaled-down canvas view of a larger browser window (1000px x 1600px), allowing users to click and determine the x/y position within the full window ...

What is the best way to maintain the selected radio button on the following page using PHP?

Image of My Project I am working with a file and need some assistance. I want to ensure that when a user selects an answer, the radio button value remains unchanged if they click back. Can someone please help me with this? while( $row = mysqli_fetch_arra ...

The ts-node encountered an issue with the file extension in this message: "TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension

When using ts-node, I encountered the following error: $ ts-node index.ts TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/projects/node-hddds8/index.ts I attempted to remove "type": "module& ...

Instructions on integrating Realm SDK with Vue3

I've been utilizing Realm SDK in Vue2 with the following syntax: // file scr/plugins/realm.js import Vue from 'vue'; import {App} from 'realm-web; Vue.prototype.realmApp = new App({id: 'artes-realm-vl12'}) //file scr/main.js ...

Setting up React Router in a nested directory with a flexible route structure

As a newcomer to react router, I am seeking guidance on setting it up in a specific scenario. Imagine we have a PHP application running on 'http://www.example.com'. Within this setup, there is a react application located at 'http://www.examp ...

An unconventional approach to utilizing Vue CLI for personal projects

Currently, I have built a Vue application with Bootstrap-Vue, Express (Node.js), and MySQL. The project is being developed using vue-cli and is in development mode. The client and server are separated into different folders to create a single-page applicat ...

Troubleshooting problems with AngularJS placeholders on Internet Explorer 9

On my partial page, I've included a placeholder like this: <input name="name" type="text" placeholder="Enter name" ng-class="{'error':form.name.$invalid}" ng-model="Name" required /> I have also set up client side validation for the ...

Incorporate CloudKit JS into Vue Application

I want to integrate CloudKit JS into my new Vue project so I can access its functions from any component within my app. As a newcomer to Vue, I appreciate your patience with me. :) Currently, I attempted adding the following code snippet in main.js: var ...

Passing events value in Fullcalendar Plugin by utilizing a javascript function to format events in the Fullcalendar event format

Currently, I am utilizing the full calendar plugin to display data on a calendar from a workflow. The values needed for the events are obtained in a separate function and need to be passed as fullcalendar events. I am uncertain about how to retrieve these ...

Issue with adding a video to a playlist using Youtube API

While I have successfully implemented GET requests using the Youtube API v3, I am encountering difficulties when trying to make a POST request to add a video to a playlist. Despite trying various approaches such as including the key in the query string, pl ...

Javascript - accessing a local file (located in the same directory as the HTML file, not an uploaded file)

I have been attempting to find a solution, but have had no success. Is there a way to read a file using JavaScript or HTML? I have a text file named "sample.txt" with some information in it. This file is located in the same folder as the HTML file that con ...

Is it better to have JavaScript and HTML in separate files or combined into a single HTML file?

Do you notice any difference when coding in both HTML and JavaScript within a single .html file compared to separating HTML code in a .html file and JavaScript code in a .js file? Does the functionality remain the same in both scenarios? For example, in t ...

When attempting to import vue.js within a JavaScript file in a Django application, an error of 'Uncaught SyntaxError: Cannot use import statement outside a module' is encountered

Currently, I am delving into incorporating vue.js into a live django project and have come across a stumbling block. After setting up npm in the venv environment and installing the vue package, my next step is to create a Vue object within a js file using ...

Developing a custom CSS for React using clamp and focus attributes

I'm currently working on creating an object to be used in inline style, but I'm having trouble figuring out how to correctly write `clamp` and `after`. const PhoneInputStyle = { fontSize: clamp("13px", "1.111vw", "16px"), /*this particular ...