Vue specialized directive

Looking to implement multiple select in Vue without using a library, I came across a useful jsFiddle code. Check it out here:

https://jsfiddle.net/02rafh8p/

To make this functionality available globally, I created a custom directive in another JavaScript file as shown below:

import Vue from 'vue';

export const Select = {
twoWay: true,
priority: 1000,

params: ['options'],

bind: function() {
    let self = this;
    $(this.el)
        .select2({
            data: this.params.options
        })
        .on('change', function() {
            self.set($(self.el).val())
        })
},
update: function(value) {
    $(this.el).val(value).trigger('change')
},
unbind: function() {
    $(this.el).off().select2('destroy')
}

};

Vue.directive('select', Select);

Next, I want to use this custom directive in my component:

<template>
   <div id="el">
     <p>Selected: {{selected}}</p>
     <select v-select="selected" multiple  :options="roles2" style="width: 400px; height: 1em;">
     <option value="0">default</option></select>
   </div>
 </template>

import {Select} from '../select.js';

export default {

    directives: {
        Select
    },

    data() {
        return {
            form: new Form({
                memberId: this.member.id,
                firstname: this.member.user.firstname,
                lastname: this.member.user.lastname,
                email: this.member.user.email,
                roles: Object.values(this.member.actual_roles),
                rate: this.member.billing.rate,
                currency: this.member.billing.currency_id,
                type: this.member.billing.type
            }),
            fullname: this.member.user.full_name,
            selected: [],
            roles2: [
                {id: 1, text: 'hello'},
                {id: 2, text: 'what'}
            ]
        }
    },
}

Encountering the following error:

TypeError: Cannot read property 'el' of undefined

After modifying a piece of code in select.js like so:

 let self = this;
  $(this.el) => hange to : $('#el')
    .select2({
        data: this.params.options
    }) ...

A new error arises:

TypeError: Cannot read property 'params' of undefined

This being my first attempt at creating a custom directive, I seek assistance in resolving these issues. Any help or advice on how to address these errors would be greatly appreciated.

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

The function from a different .js file cannot be located in NodeJS

I'm brand new to working with Javascript and NodeJS. This is the structure of my website's code: |source | | stylesheets | | | index.styl | | templates | | default.jade | | homepage.jade |static | | [.css generated] | | ...

`Some Items Missing from Responsive Navigation Menu`

Hey there! I'm currently diving into the world of responsive design and I'm attempting to create a navigation bar that transforms into a menu when viewed on a mobile device or phone. Everything seems to be working fine, except that not all the na ...

Enhancing HTML "range" element with mouse scroll functionality for incrementing values in step increments

I'm working on developing a scroll feature that operates independently from the main window's scrolling function. I aim to trigger specific events in the primary window based on interactions with this separate scrollbar. The only solution I coul ...

"Eliminate the headers of columns within the collapsible rows on the ui-grid interface

I am working with an expandable table and trying to figure out how to hide the column headers for only the expandable rows within the table. I experimented with including showHeader : false in the subGridOptions, but without success as the headers are stil ...

Discover the quick method for instantly opening a Bootstrap 5 Modal and seamlessly loading AJAX content

The issue I am facing is that the Bootstrap 5 Modal does not open immediately upon clicking the button. Instead, it waits for the AJAX Content to load first, causing a delay which impacts user experience. Is there a way to make the Bootstrap 5 modal open ...

What is the best way to display information in a newly added row of a datatables table?

Working on ASP.NET gridview conversions with datatables.net plug-in. The reason behind this is complex and subject to debate. But, I need assistance with a specific issue. The process of converting the gridview using Javascript was simple and effective. H ...

Tips for uploading information and posting it on a different page with mongoDB and Node.js

I am looking to implement a feature on a website where data is submitted to MongoDB from one page, and then retrieved and displayed on another page. The technologies I am working with include node.js, express, Mongoose, and MongoDB. Currently, the data is ...

Setting up a modern tsconfig.json configuration to enable ES6 import and export module syntax for web browsers

TLDR I am focusing on developing for the browser. Utilizing VSCode to write TypeScript and utilize a (sample) npm package. Despite my efforts, I encountered an uncaught error in the browser console stating Uncaught ReferenceError: require is not defined ...

Struggling to Conceal a Webpage: Using Position Absolute and Fixed Disrupt Layout

I am currently in the process of developing a loading image that will overlay a page when a call is initiated. My tools for this project include handlebars, HTML, CSS, and JavaScript. In my .hbs file, the structure resembles the following: <div id="s ...

Passing $index variable from a bootstrap modal window does not work in AngularJS

I'm running into a wall here. My issue involves using ng-repeat to populate a table with two buttons in each row - one for updating the row content and another for uploading files. The upload button triggers a bootstrap modal window where users can se ...

Top IDE for web development with HTML5, JavaScript, CSS, and JQuery compatibility, alongside user-friendly GUI design capabilities

Currently, I am working on a project that involves using the RGraph HTML5 canvas package for graph drawing. In addition to this, I also need to create a visually appealing GUI. While experimenting with Netbeans, I found that it lacks the necessary featur ...

Angular Digest Loop for Dynamic Photo Grid Styling

I have a special filter that alters the objects being filtered. However, when I apply ng-style="item.gridSize", it triggers my custom grid algorithm. This algorithm was adapted for my requirements from a source found at this link. angular.module("custom.m ...

Adding an overlay on hover to a flex item within a flex container

I've been working on this code and I'm trying to add a gray color overlay over the content of each flexItem, including the image. I've tried using relative position on flexCont and absolute position on flexItem overlay but it's not work ...

How can I reverse the names displayed in ng-repeat when I click?

When utilizing the orderby filter in angularjs, I want to be able to sort the data only when the button is clicked. If the button is not clicked, the sorting order should not be displayed. <tr ng-repeat="tools in toolsfilter | orderBy:orderByField:reve ...

What is the mechanism behind the operation of the inherits feature in Node.js?

The following code snippet illustrates how the inherits function works in node.js: exports.inherits = function(ctor, superCtor) { ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, ...

The debate between client-side and server-side video encoding

My knowledge on this topic is quite limited and my Google search didn't provide any clear answers. While reading through this article, the author mentions: In most video workflows, there is usually a transcoding server or serverless cloud function ...

I am attempting to retrieve JSON data from bitbns.com, however I am encountering an issue

Being new to javascript, I recently attempted to retrieve json data from bitbns but encountered the error - "(Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘’)." I scoured the internet in search of a solution, but unfortunately ...

Retrieving data from a nested array within an array of objects using JavaScript

Below is an example of an array object: const data = [ { name: "A", values: [ { name: "PASS", value: 36, }, ], }, { name: "B", values: [ { ...

Sending template reference from one Angular component to another

I have a main grid component that includes smaller grid-item components. The majority of these grid items navigate to a specific route when clicked. However, there is one particular item that should open a modal window instead of navigating. Is there a wa ...

What is the reason for the presence of additional mandatory dependencies in the package-lock.json file?

The recent release of React includes breaking changes to the TypeScript typings, causing packages that require "@types/react" with a wildcard version to automatically target this new version and break my project. Initially, I wanted to reach out to projec ...