[Vuejs]-How to I call 2 variables in Vuejs?

0👍

It’s easy to access other Vue instances variables. Although this isn’t really common practice, and I’m not really sure what you are trying to do.

Here is an example, where there is thre instances in total, where the third, gets the message variable from object one and two.

var vueObj1 = new Vue({
  data () {
    return {
      message: "vueobj1"
    }
  },
})

var vueObj2 = new Vue({
  data () {
    return {
      message: "vueobj2"
    }
  },
})

var vueObj3 = new Vue({
  el: '#app',
  computed: {
    messageFromOtherInstances () {
      return vueObj1.message + ' ' + vueObj2.message
    }
  }
})

I’ve a small codepen for you, to play with: https://codepen.io/dasmikko/pen/XWWybdr

0👍

Use data as a function and return your variables.

Read the syntax in Vue js : – https://v2.vuejs.org/v2/guide/components.html

var variable1 = new Vue({
 el: '#app1',
 data () {
   return {
    text1:"sample"
   }
 },
})

var variable2 = new Vue({
 el: '#app2',
 data () {
   return {
    text2:"sample"
   }
 }
})
<script src="https://unpkg.com/vue@2.5.9/dist/vue.js"></script>

<div id="app1">
  {{ text1 }}
</div>

<div id="app2">
  {{ text2 }}
</div>

Leave a comment