What is the best practice for importing a constant globally in my vue project, in my case it's an ENUMS.
I saw a solution by adding it in the instance of Vue like that
import { Type } from './models/Type.js'
new Vue({
router,
Type,
render: h => h(App)
}).$mount('#app')
But it's not working
const is reserved keyword you could use another name like myConst and based on this tip try :
Vue.prototype.$myConst=4;
new Vue({
router,
render: h => h(App)
}).$mount('#app')
and refer to it everywhere in your app using this.$myConst.
// ignore the following two lines, they just disable warnings in "Run code snippet"
Vue.config.devtools = false;
Vue.prototype.$myConst=4444;
new Vue({
el: '#app',
created(){
console.log(this.$myConst)
}
});
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app" class="container">
<h1>test : {{this.$myConst}}</h1>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With