Utilizing Select2 with VueJS 3 using TypeScript

I am currently delving into the world of VueJS 3 paired with typescript language. My goal is to develop a select2 component, but I encountered an error which was highlighted in the console.log message:

https://i.sstatic.net/PpUhg.png

Below is the snippet of code from my AppSelect2.vue file:

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import type { Options } from '../../interfaces';

import 'select2'
import $ from "jquery";


const emit = defineEmits<{
    (e: 'update:modelValue', value: string | number): void
}>()

interface Props {
    id?: string,
    label?: string,
    modelValue: string | number,
    options: Array<Options>,
}

const {
    id,
    label = '',
    modelValue,
    options,
} = defineProps<Props>()

function updateValue(e: Event): void {
    emit("update:modelValue", (e.target as HTMLSelectElement).value)
}

const select = ref(null)

function attach(): void {
    $(select).select2()
}

onMounted(function () {
    setTimeout(() => {
        attach()
    }, 100)
})
</script>

<template>
    <label v-if="label" :for="id" class="form-label">{{ label }}</label>
    <select ref="select" :id="id" class="form-select" v-model="modelValue" @change="updateValue">
        <option v-for="option in options" :value="option.key">{{ option.value }}</option>
    </select>
</template>

Additionally, I have installed these necessary packages :

https://i.sstatic.net/QLUMA.png

If anyone could offer some assistance, it would be greatly appreciated!

Answer №1

Did you attempt to directly import the function?

import select2 from 'select2';

Try calling it like this:

$('select').select2()

Alternatively, you could explore:
https://www.npmjs.com/package/vue-select2

This option serves the same purpose but is tailored specifically for vueJS.

You may find a solution to your issue by checking out this thread:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is causing my vue.js table to not display properly?

Struggling to render a table using vue.js? You're not alone. Many developers face challenges when trying to use v-for to iterate through data and display it in a table format. It can be frustrating when everything seems fine in the console, but the ta ...

Method for detecting changes in an AngularUI select2 dropdown

Is there a way to achieve something like the following: <select ui-select2 multiple="true" on-change="onChange()" data-ng-model="select2Model"></select> In my controller, I have the onChange(select2OnChangeData) function implemented. I at ...

Ionic3 footer using ion-tabs

Is there a way to create a common footer for all pages with 5 buttons, where the first button is selected by default? The page opened by this first button should have three tabs. I have already created the tabs but am unsure how to add the footer without r ...

Different ways to fulfill the extends type interface requirement in TypeScript

Hey there, I'm looking to create reusable hooks for API requests. Here's the code I have so far: interface DataResponse<Data> { data: Data[]; } export const useRequestInfiniteHooks = <T extends DataResponse<T>>() => { co ...

Issues with Pusher integration in Vue.js and Laravel API causing functionality to fail

Struggling with my Vue SPA and Laravel integration, I've spent hours searching online and trying various solutions to no avail. Here is the snippet from my index.html: <meta name="csrf-token" content="{{ csrf_token() }}"> Below is the subscri ...

Unable to use global modules in NestJS without importing them

Currently, I am in the process of integrating a global module into my nest.js project I have written a service as shown below: export interface ConfigData { DB_NAME: string; } @Injectable() export class ConfigManager { private static _inst ...

What is the correct way to create a new SystemError object in Node.js?

After studying Node's documentation, it is evident that SystemError is an extension of the Error class. However, when attempting to utilize SystemError directly, Node throws a ReferenceError indicating that SystemError is undefined. While it is theor ...

Chrome has decided to block the cookie

Currently, I am working on setting up a basic register/login form using vuejs on the front end and an express js server with passport library for local and social media strategies. However, I am facing issues with passing cookies to the front end when logg ...

Phaser3 encountering issues while loading files from Multiatlas

Attempting to utilize the multiatlas functionality in Phaser alongside TexturePacker. Encountering this issue: VM32201:1 GET http://localhost:8080/bg-sd.json 404 (Not Found) Texture.js:250 Texture.frame missing: 1/1.png The JSON file can actually be fou ...

Encountering a problem while deploying Vue to GitHub Pages due to issues with vue-router

I encountered some difficulties while deploying a Vue application built with vue-cli v3.0 to GitHub Pages. I utilized subtree to only deploy the dist folder to the gh-pages branch. Initially, the issue was that the assets were not being found, but I resolv ...

Building and executing an Angular 2 program on the Windows platform: Step-by-step guide

After successfully installing npm and related packages including TypeScript and Angular 2, I am encountering difficulties running Angular 2 in my browser on a Windows platform. Can someone provide a step-by-step guide to help me create and run Angular 2 ...

Getting stuck in an endless loop while making a call to Axios for data fetch with React Suspense

I am currently working on fetching data using Axios and displaying it within a suspense element. I came across a tutorial that demonstrates this process: https://dev.to/darkmavis1980/a-practical-example-of-suspense-in-react-18-3lln However, I am encounter ...

Pressing the enter key does not submit the Vue.js form

When attempting to submit the form by pressing "enter" on the keyboard, it only works when I click the "submit" button. I made sure to add @submit to the form event to ensure it triggers, but upon checking a log, I found that it only triggers when clicking ...

Exploring the Power of Laravel Socialite in Vue Single Page Applications

Seeking advice on integrating social logins through Laravel API backend with the Laravel Socialite stateless option for a single-page Vue.js app that relies solely on HTTP API calls. Encountering an issue with redirecting users back to the previous page a ...

Binding text inputs in Laravel Vue.js does not work

I am working on building a basic edit application for a post using Laravel. I am utilizing Vue.js 2 to bind the data, but I am encountering an issue where the data is not displaying as expected. When I use Vue's syntax, such as @{{ postData.title }}, ...

Automatically activate the Focus Filterfield in the ng-multiselect-dropdown upon clicking

I've integrated the ng-multiselect-dropdown package into my Angular project using this link: https://www.npmjs.com/package/ng-multiselect-dropdown. Everything is functioning as expected, but I'm looking to automatically focus on the filter input ...

Definition of Typescript:Named Function Expression (NFE)

In my current project, I am examining the JS code snippet below as part of creating a .d.ts file: BatchBuffer.js var Buffer = function(size) { this.vertices = new ArrayBuffer(size); /** * View on the vertices as a Float32Array for posi ...

What is the best way to organize text within messages?

Hey there! I have a messaging app with arrays of messages. [{"id":4, "user_id":1, "messageable_id":3, "messageable_type":"conversation", "text":"Text 1", "action":null, "target_id":null, "created_at":"2019-06-17 15:47:55", "updated_at":"2019-06-17 15:47:5 ...

Production environment experiencing issues with Custom Dashboard functionality for AdminJS

I have successfully integrated AdminJS into my Koa nodejs server and it works perfectly in my local environment. My objective is to override the Dashboard component, which I was able to do without any issues when not running in production mode. However, wh ...

Utilize prop-types inheritance when a component is rendered via props

Is it possible to inherit prop-types when a component is rendered via the parents prop, without direct access to 'ChildProps' and 'Props' interface? Parent Component interface ChildProps { counter: number; setCounter: React.Dispat ...