Struggling to grasp what should be a straightforward task, I'm open to the idea that my current approach may not be the best way forward. Currently, we are updating our processes and refining some of the functions within a .js file that houses numerous objects and functions utilized in our applications.
To illustrate, consider the following snippet:
**COMMON.JS**
const dynamicYearColumn= {
resizable: false, suppressMovable: true, sortable: true, width: 100, menuTabs: [], type: 'numericColumn',
}
const defaultPercentageColumn= {
resizable: false, suppressMovable: true, sortable: true, width: 150, menuTabs: [], type: 'numericColumn',
valueFormatter: formatPercent,
cellStyle: { 'text-align': 'right' },
cellClass: function (params) { var className = numberToColorClass(params.value); return className }
}
function formatPercent(number) {
if (isNaN(number.value) == true) { return '-' }
return isFinite(numberWithCommas(parseFloat(number.value, 2).toFixed(this.defaultDecimalRounding))) ? numberWithCommas(parseFloat(number.value, 2).toFixed(this.defaultDecimalRounding)) + '%' : '?';
}
function numberWithCommas(n) {
var parts = n.toString().split("."); return parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
}
In my main.js file, I have implemented:
import common from './scripts/common.js'
const commonLibrary = {
install() {
Vue.common = common
Vue.prototype.$common = common
}
}
Vue.use(commonLibrary)
However, I am facing difficulties in implementing this setup.
If I enclose the code within export default
in my common.js, the code needs to be modified, resulting in errors with unrecognized functions like formatPercent
.
If I use
export {dynamicYearColumn,defaultPercentageColumn}
, it partially works but the functions remain undefined.
Creating a Mixin also yields similar results, with internal functions being unrecognized, along with advice against loading a large library into Mixins due to poor coding practices.
While there are plenty of resources with examples available, I struggle with the terminology to search for relevant solutions.
In essence, all I need is seamless access to an extensive collection of objects and functions from a single JS file, callable from any component within my Vue SPA.