Skip to content

Instantly share code, notes, and snippets.

@Sovai
Forked from vielhuber/Component.vue
Created July 27, 2021 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sovai/279abcf52ff471aeacce4e30a5f2eced to your computer and use it in GitHub Desktop.
Save Sovai/279abcf52ff471aeacce4e30a5f2eced to your computer and use it in GitHub Desktop.
global helpers helper functions #vue
<template>
<div :test="$helpers.foo1()" :test2="bar()"></div>
</template>
<script>
import { bar } from '@/helpers/utils';
</script>
// src/helpers/utils.js
// these functions can be used without importing
export default {
foo() {
return 'foo';
}
};
import helpers from './helpers/global';
Vue.use({
install() {
Vue.helpers = helpers;
Vue.prototype.$helpers = helpers;
}
});
/* usage inside components: */
this.$helpers.foo()
/* usage anywhere else: */
Vue.helpers.foo()
// src/helpers/utils.js
// these functions can only be used after importing
function bar() {
return 'bar';
}
export { bar };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment