Pass props to child components in Vue.js based on certain conditions

Here's an example of conditionally sending props to the Quantity component.

<v-card
 class="mx-auto"
 max-width="344"
 shaped
 v-for="(item, index) in allItems || orderedItems"
 :key="index"
>

<Quantity {allItems ? :Items = item : :Ordered = item} />

</v-card>

If orderedItems are activated, I want to send the Ordered props. If allItems is activated, then I will send the Items props.

Answer №1

opt for utilizing v-bind in place of:

<Quantity v-bind="quantityProps" />

Next, devise a function to handle this:

quantityProps(product) {
  return {
    [this.allProducts ? 'Products' : 'Ordered']: product
  }
}

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

Choose a property and its corresponding value at random from a JavaScript object

Recently delving into the world of Javascript and seeking guidance. I set out to create a random picker from an array and succeeded with the following setup: var movie = ["star wars", "lotr", "moonlight", "avengers"] function newMovie() { var randomNu ...

Issue with ESLint arises following the installation of npm create-react-app package

ESLint is showing Invalid Options: - Unknown options: env, parserOptions, rules. The 'parserOptions' has been removed and you should now use the 'overrideConfig.parserOptions' option instead. Similarly, the 'rules' have been r ...

Is it possible to utilize AJAX and JavaScript exclusively for server-side scripting?

Currently in the process of learning PHP, I've decided to analyze the code using a language that I am more comfortable with as my approach. I'm attempting to recreate a PHP dashboard with features such as role management and session logging. Ha ...

Functions properly in JSFiddle, but fails in the browser even when surrounded by a document ready handler

I am struggling with printing a 16x16 grid of divs using jQuery. It works perfectly on JSFiddle, but only one row is printed when testing in the browser. I realized that changing the jQuery version from edge to 2.1.3 on JSFiddle results in the same issue i ...

Avoid using preventDefault() to avoid interrupting AJAX requests to external PHP files

I created an AJAX request to upload a file and add its details to a MySQL database using a form. The code is functioning well, but encounters issues when I include preventDefault() in the submit event to avoid being redirected to the external PHP file. ...

Issue with Node.js Mongoose: Attempting to store a date in UTC Timezone, but it is being saved in the local time

I am relatively new to using nodejs and mongodb. I am encountering an issue with dates while using mongoose for mongodb - whenever I attempt to save a date into the database, it automatically gets converted to the local timezone, which is not the desired b ...

Tips for creating a preloading screen before your webpage is fully loaded

I am interested in creating a preloader bar that displays the loading progress of a webpage before it is completely loaded, similar to the example on pushcollective.com where there is a progress bar at the top of the page. Is there a clean and efficient w ...

Assign a variable name to the ng-model using JavaScript

Is there a way to dynamically set the ng-model name using JavaScript? HTML <div ng-repeat="model in models"> <input ng-model="?"> </div JS $scope.models= [ {name: "modelOne"}, {name: "modelTwo"}, {name: "modelThree"} ]; Any ...

Use jquery to modify fields

Looking to modify certain contact form fields using jQuery. Here is the original code: <label for="avia_3_1">Name <abbr class="required" title="Required">*</abbr></label> This is the jQuery I have implemented. There is a dropdown ...

Adding a prefix to all specified routes in Express.js

Imagine having an Express app defined in a file called server.js as follows: const app = express(); app.use('/foo', foo); app.use('/bar', bar); module.exports = app; You then import this Express app into another file named index.js: ...

Exploring Websites Using Javascripts or Online Forms

I am currently facing a challenge with my webcrawler application. It has been successfully crawling most common and simple sites, but I am now dealing with websites where the HTML documents are dynamically generated through forms or JavaScripts. Even thoug ...

Having trouble with Socket.emit() and socket.on() not functioning properly?

I am currently developing a game using node.js and socket.io. I am attempting to send a variable to the client using socket.emit() and socket.on(). Despite my efforts, it doesn't seem to be working as expected and I'm unsure of what the issue mig ...

Using Computed Values in Vue.js for Asynchronous Calls

I am working on setting up a dynamic array within the computed: section of my .vue file. This array consists of a list of URLs that I need to call individually (within a repeated component) in order to fetch articles. These articles can vary, so the comput ...

Karma has encountered an error: The variable "angular" cannot be found

For the past week, I've been attempting to run Karma in my project. I followed a tutorial on AngularJS Unit Testing, but when I execute karma start, an error appears in the console: PhantomJS 2.1.1 (Linux 0.0.0) ERROR ReferenceError: Can't fin ...

Is there a way to compel a react component to refresh from a separate JavaScript file?

I'm facing an issue where a React component is not displaying a list of items fetched from the backend using websockets. Since my websockets.js file is outside of the React hierarchy, the list never gets refreshed and therefore the items are not shown ...

Hover over buttons for a Netflix-style effect

https://i.stack.imgur.com/7SxaL.gif Is there a way to create a hover effect similar to the one shown in the gif? Currently, I am able to zoom it on hover but how can I also incorporate buttons and other details when hovering over it? This is what I have ...

TinyMCE - Optimal Approach for Saving Changes: keyup vs onChange vs blur

In the context of my Filemaker file, I am utilizing the TinyMCE editor. My goal is to automatically save any changes made by the user, whether it's typing, applying formatting, inserting an image, or making any other modifications. I have a function ...

Using asynchronous import statements in Vue allows for more efficient loading of components

I am using Vue and have a method in my file called load.js. async function loadTask(x) { return await x; // Some async code } export { loadTask }; In one of my Vue components, I call the method but encounter an issue where the use of await prevents the ...

Can a function be invoked using a variable as a parameter in JavaScript?

Imagine a scenario where the need arises to use a conditional statement that performs the same action but calls a different method. const c = true; // just for illustration let b = ''; if (c) { b = 'method1'; } else { b = 'met ...

Dividing a string using regex to deal with numerical issues

My task involves processing a list of strings that look like this: Client Potential XSS2Medium Client HTML5 Insecure Storage41Medium Client Potential DOM Open Redirect12Low The goal is to split each string into three parts, like so: ["Client Potential X ...