Transferring information between two Vue components using a variable declared in the main JavaScript file

In my project, I am working with two separate Vue files - let's say 1.vue and 2.vue - along with a main JS file.

Within my main.js file, there is a variable xyz under the window.App data method.

I am looking to utilize this variable as a shared data variable that can be accessed by both of my Vue files. For example, 1.vue will assign some data to this variable, and 2.vue will be able to access this data.

Answer №1

There are a couple of effective methods for transferring data between components (for example, 1.vue and 2.vue).

  1. One way is to utilize props and events. Define a variable like xyz in your root instance data. Then, in 1.vue, pass this value as a prop and update it as needed. By triggering an event when the value changes in 1.vue, you can ensure that the root instance updates, allowing 2.vue to access the updated value.

  2. Another option is to use vuex. Vuex establishes a shared state accessible to all components via a store. Through mutations and actions, you can modify the value, and other components will dynamically respond to these changes using a computation.

Answer №2

As stated in the guidelines

When transferring data between components in Vue, it is recommended to use props in the child component and v-bind or : in the parent component.

Example of Parent Vue :

<child-component :data="example"></child-component>
<script>
export default {
  data: () => ({
    example: [1,2,3]
  })
</script>

Example of Child Vue:

<div v-for="item in example"></div>

<script>
export default {
  props: {
    example: Array // Array is the data type (can be Object, Array or String)
  }
</script>

Just a heads up: I found it a bit challenging to get everything functioning correctly.

Edit : Alternatively, a mixin class could be used to manage and update the variable

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

Vuex Action never returns a resolved value

I'm encountering an issue with getting my Action to resolve the promise properly. I've gone through what appear to be the most relevant posts. Returning Promises from Vuex actions I need to know when my action has finished so that my component ...

Unfortunate escapement of characters discovered in variable that returns JSON image source

I am currently utilizing Google Tag Manager to incorporate the 'article' JSON markup and retrieve different variables for modifying specific sections on particular pages. Among the elements I am attempting to obtain is the image source. At prese ...

What is the best way to update a form after it has been submitted?

i have a form that looks like this <form id="abc"> <div> <select > <option id= "1"> ha1 </option> <option id= "1"> ha2 </option> <option id= "1"> ha3 </option> <option id= "1"> ha4 </option> ...

Error: content within v-html directive not displaying as expected

The v-html content is not rendering correctly for me. Interestingly, when I remove the v-html in the first list item, it works fine. Take a look at my code below: <div> <br> <li v-html="`a`"> <ul> <li ...

Learn how to create a web application by utilizing HTML5 and REST, and then seamlessly transfer the front-end code to a mobile app using Cordova

Can a mobile application be developed from a web portal using Cordova or similar frameworks? The web portal will consist of: HTML5 for front-end J2EE for back-end with JAX-RS Is it feasible to extract pages from the web portal and integrate them into a ...

Creating a placeholder for VueJs async template/component rendering

As a novice to VueJs, I am still navigating my way through its intricacies. One challenge I'm currently facing is loading templates asynchronously from the database. To tackle this, I have adopted the component-factory approach for my components. var ...

How can one achieve the equivalent of Flask Safe when making an ajax call?

Having trouble replicating equivalent functions in my Ajax call as I can in regular Javascript on my main HTML page. Using Python/Flask at the back-end. Any similar methods to use the {{ variable | safe }} syntax in AJAX for similar results? My code snipp ...

What techniques can be employed to utilize multiple JavaScript files?

Hey there, I am facing an issue while trying to execute multiple JavaScript codes. My first script is running smoothly with the change function, but the second one seems to be causing some trouble. Can anyone guide me on how to effectively run multiple J ...

Transform pixel padding into percentage ratios

I've been searching through various discussions to find a solution, but none of them seem to fit my particular scenario. My issue involves a hyperlink with absolute positioning inside a div with relative positioning. The padding and margins are curre ...

Discovering the value of an item when the editItem function is triggered in jsGrid

Within my jsGrid setup, I have invoked the editItem function in this manner: editItem: function(item) { var $row = this.rowByItem(item); if ($row.length) { console.log('$row: '+JSON ...

Missing table header in HTML causing table to not display correctly

I currently have an HTML table that consists of three columns and a varying number of rows, depending on the output from a database. var fields = ['a','b','c']; //variable from database var data = ['p', & ...

Sort through the JSON Array based on a specific key/value pair

I have some code here, and I'm trying to only display items with the tag "assistance" and not the others. I'm struggling with how to achieve this. Can anyone help me out? function displayall(newid){ $.ajax({ url: "https://cubber.zendesk. ...

Show information in a table based on a unique identifier

I am working with some data that looks like this [ { date: '20 Apr', maths: [70, 80.5, 100], science: [25, 20.1, 30] }, { date: '21 Apr', maths: [64, 76, 80], science: [21, 25, 27] }, ]; My goal is to present ...

Obtaining a substantial pdf file using html2pdf

While using html2pdf to generate a PDF of my website, I noticed that the downloaded PDF ends up being 14 pages long. However, after approximately 12 pages, all the colored elements seem to disappear. On mobile screens, this issue occurs even sooner, around ...

Menu toggle vanishes suddenly

Whenever I click the toggle button, I notice that the menu (which I have set to appear on toggle) only shows for a brief moment before disappearing. As a beginner in bootstrap, I am sure I might have made some obvious mistakes. Any assistance would be gr ...

The process for retrieving an environment variable within a pipeline and transferring it to a release pipeline

My program has an automatic build and deploy process. Here is the current workflow: When I push code to Git, Azure's build pipeline generates an artifact. This artifact is then picked up by Azure's Release pipeline, which deploys it to a Digi ...

The feature to prevent multiple selections in JSTree is not functioning as expected

Incorporating JSTree into my application involves the code below. this.CreateTreeView = function () { $('#jstree_demo_div').jstree({ 'core': { 'multiple': false, 'data': [ ...

How can I prevent a hyperlinked element from being clicked again after it has been clicked using JavaScript or jQuery in PHP

I am struggling with disabling the href after it has been clicked. Can someone please assist me with this? It is crucial for me to complete this PHP program. Style.css .disabled { pointer-events: none; } ...

Assigning the style of the parent element based on the child element

Currently, I am attempting to develop a customized dropdown field and here is the code I have come up with: const list = document.querySelector('.list'); const listItems = list.getElementsByTagName('p'); document.querySelector('# ...

Utilizing _.partial on a single-argument function

Currently, I am in the process of refactoring code within a react.js application. There is an element that utilizes Underscore.js _.partial on a function that already has one argument. Is there any real benefit to doing this? I can see how it works based ...