How do I declare a global variable in a project generated by vue-cli?

how do I declare a global variable in a project generated by vue-cli? My personal understanding is that main.js is an entry file and App.vue is a root-level component. If a variable is declared here, webpack will be packaged recursively. But why can"t this variable be referenced in other .vue script?


declare your variables in main.js , then Vue.prototype.xx = xx;
other vue files this.xx can be accessed,
you can also write them into a js file, and then import xx from "xx" in main.js , Vue.prototype.xx = xx; `


landlord, hello! This is fine. You can define a global variable in main.js, such as window.userName = 'hello' . It is OK to use window.userName directly on other pages. A personal test is fine.


global variable? vuex?


try writing in index.html, either you write in data on the page or you write in store

Menu