Vue issue causing problems with Rangy library: "..." throwing a function error specifically within hooks

Utilizing the Rangy library in a Vue component enables me to highlight individual words within text. Everything functions smoothly when Rangy is invoked from any method within the methods object:

let applier = rangy.createClassApplier('some-class');
applier.toggleRange(range);

However, I encountered an issue when attempting to restore a specific state upon page load by using the same method inside the created and mounted hooks.

The initial lines of the code in my component currently appear as follows:

import _ from 'lodash';
import rangy from 'rangy';
import 'rangy/lib/rangy-classapplier';

export default {

    mounted: function() {
        // Exit the function if no data needs to be restored from the database
        if (_.isEmpty(this.mostRecentAnswers))
            return;

        else {
            // ERROR
            let applier = rangy.createClassApplier('some-class');
            // ...
        }
    }
    // ...
}

An error message stating

TypeError: rangy__WEBPACK_IMPORTED_MODULE_2___default.a.createClassApplier is not a function at VueComponent.mounted
is displayed.

I am utilizing Vue with Laravel and Laravel Mix (which serves as a wrapper for Webpack).

What could be causing this issue in my code? It's worth mentioning that I import Lodash without encountering any errors.

Answer №1

If you want to simplify things, I suggest turning this into a Vue plugin:

// plugins/vue-rangy.js

import rangy from 'rangy'

const VueRangy = {
  install (Vue, options) {
      if (options.hasOwnProperty('init') && options.init) {
        rangy.init()
      }

      Vue.prototype.$rangy = rangy
  }
}

export default VueRangy

Then utilize it just like any other plugin:

import VueRangy from 'plugins/vue-rangy'

Vue.use(VueRangy, {
  init: true
})

After that, simply access this.$rangy in your components.

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

Sequelize is unable to retrieve a table from the database

I am configuring Sequelize in order to streamline the manipulation of an MSSQL database. My attempt to define a table called 'Stock' has resulted in unexpected behavior when trying to query it. Below is the code snippet I used for defining the t ...

Clicking on the ActionButton will trigger the sending of an HTTP Request in this Firefox

Seeking guidance: As I develop an addon, my objective is to initiate a HTTP request upon clicking the ActionButton. The data to be sent to the server includes the URL of the tab and the content of the page. Initially, I attempted to create a log message a ...

Updating the file path for JS and CSS files in the finalized build

Looking for some guidance here. I'm trying to deploy a vuejs app using Vue CLI 3 and when I run the build command, the files are built into the dist folder as expected. However, there are js and css folders inside dist that contain the respective file ...

JavaScript - convert the values of an array within a JSON object into separate strings

I am receiving a JSON object from an API, and my next step involves some string analysis of each key value. This process works perfectly for 90% of the objects I receive because the key values are strings. { ID: '0012784', utm_source: 'webs ...

Unable to construct Vue application after integrating webpack extension

Currently facing an issue with adding a webpack extension to build my Vue app. Despite having Vue/cli which includes webpack, I have also attempted to install different versions of webpack without success. Anyone experienced the same problem and found a so ...

Implement the use of `require` to update the `$watch` in an

As a new Angular user, I am facing an issue that involves having two directives in my HTML code like this: <parent-dir param="par"> <child-dir></child-dir> </parent-dir> And defining them in my JavaScript code as follows: In t ...

Enumeration field with Conditional logic

I am currently developing a Content Management System (CMS) using strapi for a client, and I want to provide them with the ability to control the questions included in a questionnaire. Each question will be categorized under different sections in the quest ...

The module ~/assets/images/flags/undefined.png could not be found in the directory

When I use the img tag with a dynamic address filled using require, it works well in the component. However, when I try to write a test for it, an error is thrown. console.error Error: Configuration error: Could not locate module ~/assets/ima ...

Incorporating min.js file into a ReactJS application

I have received a min.js file from a third-party source and now I am looking to integrate it into my existing ReactJS project. While exploring some sample codes on how to start using their third-party features, I noticed that each example follows a simila ...

Error encountered in Three.js: ShaderPass.js is unable to read the property 'prototype' of undefined

I'm currently facing an issue while trying to implement a basic GlitchPass in a Three.js demo. I keep encountering the error message Uncaught TypeError: Cannot read property 'prototype' of undefined at ShaderPass.js. For this particular dem ...

Guide to updating a SQL value via a button click using PHP

Unsure of the amount of code required for my project, so any guidance is appreciated! I am currently working on the admin-end of a PHP project that utilizes SQL. The admin has the ability to update, remove, and promote normal users to administrators (some ...

Error message in Mysql connector for NodeJS: Pool has been closed

We have developed a NodeJS application that functions as an API for a frontend Angular project we are currently working on. So far, both components have been operating smoothly. However, occasionally we encounter a strange error with the API stating "Pool ...

Achieve full height without scrolling in React

I am facing an issue where the height:100% property is not working to fill the remaining area on the screen. A red outlined area represents the border radius, while the highlighted yellow space should have been filled: https://i.stack.imgur.com/ZQNkw.png ...

Encountering an error stating that 'coordinates should consist of an array with two or more positions'

Utilizing turf.js to generate a line depicting the path of an individual while their location is tracked. An array of coordinate arrays resembling Turf.js (lineString) is causing this error: Uncaught Error: coordinates must be an array of two or more posi ...

Identifying iOS versions below 5 using JavaScript

There is an issue with the "fix" for position:fixed in older versions of iOS. However, when iOS5 or higher is installed, this fix actually disrupts the page. I am aware of how to identify iOS 5 using this code: navigator.userAgent.match(/OS 5_\d like ...

What is the best method to display PHP POST data with AJAX without using jQuery?

Essentially, I have this HTML code: <form method="post" id="myform" onsubmit="getData()"> <fieldset> <legend>Form</legend> <label>Color: <input type='text' id="color" name='color'></label> < ...

What is the best method to deactivate additional choices and add inputs to several dropdown menus containing identical values using angular js?

Would like to have 3 dropdown lists with identical values. The first dropdown must be selected, while the other two are optional. All three dropdowns will have the same options. If a user selects an option in dropdown 1, it will become disabled for the re ...

Optimize your mobile experience by creating a notification menu that is scrollable and fixed in position

Recently, I purchased Moltran, but encountered a major issue: The notification menu disappears on mobile devices, which is not ideal for me. After some research, I discovered that removing the hidden-xs class from the li notification element can solve this ...

Steps for deactivating AMD on four files and sequentially loading them using webpack

I am facing an issue where I need to disable AMD on 4 files and ensure that video.js is loaded before the other 3 files, as they are dependent on it. My attempt to achieve this in webpack.config.js was unsuccessful: const path = require('path') ...

The Angular controller is defined in a separate file such that the controller file is loaded before the Module file

I have made progress in the following areas: I successfully created a module using the correct convention: var myApp = angular.module('myApp', []);. I ensured to include the empty array as the second parameter. In addition, I developed some con ...