Skip to content

Instantly share code, notes, and snippets.

@jam2-hey
Last active August 28, 2020 02:39
Show Gist options
  • Save jam2-hey/245e6a6e5fae80aeefde8854e7a8ad4c to your computer and use it in GitHub Desktop.
Save jam2-hey/245e6a6e5fae80aeefde8854e7a8ad4c to your computer and use it in GitHub Desktop.
Inject constants to Vue instance

Install

Vue.use(IncludeConstants);

Define

// In components options
{
  $consts: {
    MAX_LENGTH: 40
  }
}

Using (Template)

<input max-length="MAX_LENGTH">

Using (Script)

this.MAX_LENGTH
export default {
install (Vue) {
Vue.mixin({
created: function () {
if (this.$options.$consts) {
Object.entries(this.$options.$consts)
.forEach(([key, value]) => {
Object.defineProperty(this, key, {
value,
writable: false
});
});
}
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment