vue.js - Apply global variable to Vuejs -
i have javascript variable want pass globally vue components upon instantiation either each registered component has property or can accessed globally.
note:: need set global variable vuejs read only property
you can use global mixin affect every vue instance. can add data mixin, making value/values available vue components.
to make value read only, can use method described in this stackoveflow answer.
here example:
// global mixin, applied every vue instance vue.mixin({ data: function() { return { globalreadonlyproperty() { return "can't change me!"; } } } }) vue.component('child', { template: "<div>in child: {{globalreadonlyproperty}}</div>" }); new vue({ el: '#app', created: function() { this.globalreadonlyproperty = "this won't change it"; } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.3/vue.js"></script> <div id="app"> in root: {{globalreadonlyproperty}} <child></child> </div>
Comments
Post a Comment