You need to export the variable, and then import it into another file where you want it. The best way to do that would be to put the variable a
in its own module file. This allows you to avoid using global variables, which pretty much negate the purpose of modules!
a.js:
export const a = 'foo'
App.vue:
<script>
import { a } from './a.js'
console.log(a) // foo
console.log(1)
export function log3() {
console.log(3)
}
</script>
main.js:
import { log3, default as App } from './App.vue'
console.log(2)
log3()
new Vue({
el: '#app',
render: h => h(App)
})
import { a } from './a.js'
console.log(a) // foo
Here is what will be logged to the console:
- 'foo' (from App.vue)
- 1 (from App.vue)
- 2 (from main.js)
- 3 (from main.js calling the function log3 from App.vue)
- 'foo' (from main.js)
Now both App.vue and main.js have access to a
, because they have explicitly imported it. The fact that App.vue has access to a
has nothing to do with the fact that main.js
also has access to a
.