1

I have two separate vue files:- for example 1.vue and 2.vue and a main js file.

I have a variable xyz under window.App data: method in main.js

I want to access this variable as a share data variable how will I use it in my both the vue files. for example 1.vue will put some data under this variable and 2.vue will access this data.

3
  • 1.vue will emit an event, main will update the value, and 2.vue will receive it as a prop. See vuejs.org/v2/guide/components.html#Composing-Components Commented Jan 3, 2018 at 14:43
  • How it will work for global data:- I mean, in main.js window.App = new Vue({ el: '#app', data: { xyz: {} }, and 1.vue put data in xyz and 2.vue use it .. I dont understand how to create a method in 1.vue to access this variable Commented Jan 3, 2018 at 14:55
  • Components should not access variables that are not their own. Just because App is global does not mean its data should be treated as global. It should not. Encapsulation is important. Components should not depend on things that are external to them. Commented Jan 3, 2018 at 15:50

2 Answers 2

1

There are two recommended ways to pass data between components (eg 1.vue and 2.vue)

  1. Using props and events. You have xyz defined in your root instance data. When using the component declared in 1.vue, pass the value as a prop (property) and add this as a value as a prop in 1.vue. When the value changes in 1.vue, trigger an event which updates the value in the root instance, so that 2.vue can also see this value through a prop.

  2. Use vuex. Vuex creates a shared state that all components can see via a store. You update the value through mutations and actions, and other components will automatically react to these changes using a computation.

Sign up to request clarification or add additional context in comments.

Comments

0

According to the documentation

If you need to pass datas from a component to another. You should use props in the child component and v-bind or : in the parent.

Parent Vue :

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

Child Vue:

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

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

PS : It was a bit hard for me to make it works.

Edit : Or maybe you can use a mixin class to create and update the variable

4 Comments

How it will work for global data:- I mean, in main.js window.App = new Vue({ el: '#app', data: { xyz: {} }, and 1.vue put data in xyz and 2.vue use it .. I dont understand how to create a method in 1.vue to access this variable
I think it should work this way. That's how I do with Vue Router. Can't you acces xyz in other component ? Try to type "window.app.data.xyz" in the console. Otherwise, you can attach your data to the Global Scope. To the window by example (if datas is not security sensitive)
Is it possible to create method in 1.vue like this getdata: function(app){window.App.xyz = app; '2.vue'; }
Probably, but it's a bit ugly, I would try to do it with Vue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.