Answer the question
In order to leave comments, you need to log in
How to make global variables and method in VueJs2?
Hello!
In Vu how to make global variables and method or how to use own methods or variables in any component?
for example i have in main.js
new Vue({
el: '#app',
data:{
globalVar: 'my variable',
},
methods:{
myGlobalMethods(){
// code
}
}
});
Answer the question
In order to leave comments, you need to log in
You can generally organize it like this:
Storage file:
// store.js
export default {
a: 'foo',
b: 'bar'
}
import Vue from 'vue'
import Store from './store'
new Vue({
el: '#app',
data: Store,
render: h => h(App)
})
<template>
<div>{{$root.a}}</div>
</template>
<script>
import Store from './store'
export default {
name: 'app',
methods: {
test () {
this.$root.a = 'something value'
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question