I am currently exploring the integration of the jQuery Plugin Chosen into my vue.js/Webpack project with TypeScript.
After some research, I discovered that it is recommended to encapsulate the plugin within a custom Vue component.
To kick things off, I made sure to install the necessary NPM packages:
npm install jquery --save
npm install @types/jquery --save
npm install chosen-js --save
npm install @types/chosen-js --save
Next, let's take a look at my Vue component:
<template>
<select>
<option value="1">Test1</option>
<option value="2">Test2</option>
</select>
</template>
<script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator";
import $ from 'jquery';
import 'chosen-js';
@Component
export default class ChosenSelect extends Vue{
@Prop()options!:string;
@Prop()value!:string;
mounted() {
let vm = this;
let el = $(vm.$el);
console.log(el);
}
}
</script>
So far, everything seems to be in order. However, when attempting to utilize the Chosen functionality by adding import 'chosen-js', I encountered an issue where I received a
Uncaught ReferenceError: jQuery is not defined
error from the chosen library.
My ultimate question now becomes: What is the proper approach to importing both jQuery and chosen-js while effectively using them within a Vue Typescript component?