I want to reuse a javascript library I did some time ago in a vue.js component.
The js library works like this:
- simple reference on the main script with tag
- css loading
The library provides a constructor, so what is needed is a element with an ID and to init the component in javascript I only need to:
var divelement = new pCalendar("#divelement", {
... various options
});
I'm trying to create a .vue component that is able to do the same (loading js library, init css, init component with the constructor and options), but I can't figure out what is the right way to do it.
This is what I'm working on, but in this situation I get an error because pCalendar is not recognized as constructor.
<template>
<div id="myelement"></div>
</template>
<script>
import perpetual_calendar from '../../../assets/js/perpetual-calendar/perpetual_calendar.js'
export default {
data () {
return {
myelement: '',
}
},
mounted(){
var myelement = new pCalendar("#myelement",{
... various options
});
} ,
}
</script>
<style lang="css">
@import '../../../assets/js/perpetual-calendar/pcalendar_style.css';
</style>
Edit 1 (answer to @Daniyal Lukmanov question):
perpetual_calendar.js looks like this:
var pCalendar = function (element, options) {
this.options = {};
this.initializeElement(element);
this.initializeOptions(options || {});
this._create();
}
...
pCalendar.prototype.initializeElement = function (element) {
var canCreate = false;
if (typeof HTMLElement !== "undefined" && element instanceof HTMLElement) {
this.element = element;
canCreate = true;
} else if (typeof element === "string") {
this.element = document.querySelector(element);
if (this.element) {
canCreate = true;
} else {
canCreate = false;
}
} else {
canCreate = false;
}
if (canCreate === true) {
if (document.getElementsByName(this.element.id+"_store").length!=0) {
canCreate = false;
}
}
return canCreate;
};
and so on ...
Edit 2: this is the initializeOptions function, that is throwing the TypeError: "this.element is null" error.
pCalendar.prototype.initializeOptions = function (options) {
// begin hardcoded options, don't touch!!!
this.options['objectId'] = this.element.id;
this.options['firstMonth'] = null;
(... various options)
// end hardcoded options
for (var key in this.defaultOptions) {
( ... loop to load options - default one or defined by user in the constructor)
}
};
perpetual_calendar.jslook like? And what ispCalendarif you importperpetual_calendar?