Created
September 18, 2016 00:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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