B
B
bqio2018-10-27 19:16:06
webpack
bqio, 2018-10-27 19:16:06

Global variables in Vue + webpack?

An instance is created in main.js:

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

And there is a script section in the component in another file:
<script>
import { news, projects } from '../assets/db.json'
export default {
  name: 'Home',
  data () {
    return {
      news,
      projects
    }
  }
}
</script>

How can I create a global variable in an instance so that it can be used in a component? (and in template too)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sokhin, 2018-11-10
@roamn

Hello.
How to add a global property to all instances.
https://ru.vuejs.org/v2/cookbook/adding-instance-p...

import Vue from 'vue';

Vue.prototype.$appName = 'Моё приложение';

...

new Vue({
  beforeCreate: function () {
    console.log(this.$appName);
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question