Skip to content
Advertisement

How do I create a function library which I can used across all my Vuejs components?

I currently writing a financial application using Vue.js and Vuetify. I have a few component files and javascript files like

JavaScript

I have some functions which I need to use across all the Vue.js and javascript files. Would it be possible for me to perhaps write a function library which can be used across all the component and js files.

JavaScript

I know in C it is very simple just #include<financial.h> was wondering is there something similar in javascript.

Thanks.

Advertisement

Answer

There are 3 ways to do this:

1/You can create a helper.js file and import it to .vue files

// helper.js

JavaScript

// Dashboard.vue

JavaScript

2/Another way is bind the function to Vue prototype in main.js

JavaScript

then in Dashboard.vue just call this.$moneyFormat(num). No need to import anything

3/ Use mixins. You can search online on how to use this https://v2.vuejs.org/v2/guide/mixins.html

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement