Skip to content

Instantly share code, notes, and snippets.

@gianlucacandiotti
Created September 18, 2016 00:42
Show Gist options
  • Save gianlucacandiotti/0d1beb7700a43362e3d74a1c33413baa to your computer and use it in GitHub Desktop.
Save gianlucacandiotti/0d1beb7700a43362e3d74a1c33413baa to your computer and use it in GitHub Desktop.
Using global variables inside vue components with the help of mixins and default props.
// html
window.injectors.baseUrl = '<?= urlHelper('') ?>';
// mixins/injectors.js
const injectorsObj = {};
// eslint-disable-next-line no-undef
Object.keys(window.injectors).forEach((injector) => {
injectorsObj[injector] = {
default() {
// eslint-disable-next-line no-undef
return window.injectors[injector];
},
};
});
export default {
props: injectorsObj,
};
// componentsHello.vue
<script>
import injectorsMixin from 'assets/js/mixins/injectors';
export default {
mixins: [injectorsMixin],
data() {
return {
msg: 'Hello World!',
};
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment