Could someone explain to me how I can import our internal "commonFunction.js" file into a new Vue SPA and call each of those functions?
I've spent the last 2 days reading up on import, require, creating a script load, adding the .JS file to index.html, etc, etc and simply nothing is working.
I've many components in our Vue app, and we have a host of functions that handle the data based on many rules but I'm stuck at the first hurdle.
I basically want to load the commonFunctions.js file into memory and have many components access many functions and methods within that imported .js file.
I've managed to get jQuery running with a simple:
const $ = require('jquery');
window.$ = $;
Tips really would be appreciated!
Example of function to call:
function formatPercent(number) {
if (isNaN(number.value) === true) { return '-' }
return isFinite(numberWithCommas(parseFloat(number.value, 2).toFixed(defaultDecimalRounding))) ? numberWithCommas(parseFloat(number.value, 2).toFixed(defaultDecimalRounding)) + '%' : '∞';
}
As a test, I have a file called "commonFunctions.js" and the following:
module.exports = {
methods: {
method: function () {
alert("1")
},
otherMethod: function () {
alert("2")
}
}
}
On my main.js I have the following line which I believe is imported the above .js file and assigning it to a variable called myMethod
var MyMethods = require('./commonFunctions.js');
And finally, within one of my methods on the .vue page, after the user has clicked a button, I wish to test things are working with the following:
<template>
<div class="home">
<h1>{{ msg }}</h1>
<p>Welcome to your new single-page application,, built with <a href="https://vuejs.org" target="_blank">Vue.js</a>.</p>
<v-btn v-on:click="redraw">T1</v-btn>
<v-btn v-on:click="redraw2">T2</v-btn>
</div>
</template>
<script>
import { EventBus } from "../event-bus";
export default {
name: 'Home',
props: {
msg: String
},
methods: {
redraw: function () {
let $ = window.$;
EventBus.$emit("categories", [{ name: 'n1' }, { name: 'n2' }]);
EventBus.$emit("tabledata", [{ name: 'n1', Y2013: 159, Y2014: 6.0, Y2015: 24, Y2016: 4.0, Y2017: 1 }]);
$('body').css('background-color', 'grey');
MyMethods.method
},
redraw2: function () {
EventBus.$emit("categories", [{ name: 't3' }, { name: 't3' }]);
EventBus.$emit("tabledata", [{ name: 't3', Y2013: 159, Y2014: 6.0, Y2015: 24, Y2016: 4.0, Y2017: 1 }]);
}
}
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>