Exploring Vue.js: Leveraging External JavaScript/JQuery Functions

I am currently working on developing a lightbox/modal feature using Vue.js. After making significant progress, I have encountered the need to utilize existing functions from another Javascript/Jquery file. Due to the complexity of these functions, rewriting them in Vue is not a feasible option at this stage. The challenge I am facing now is how to access and integrate these functions into my project. I am unsure if I should import them as mixins or explore alternative approaches.

Presently, the code triggers an error message:

[Vue warn]: Property or method "lightbox" is not defined on the instance but referenced during render.

Here is a snippet of relevant code from my Vue component:

<template>
  ...
  <i
    class="fa fa-info-circle"
    name="moreInfo"
    aria-hidden="true"
    @click="lightbox.showBasketHelpLightbox('servicePlan', true, 3)"
  />
  ...
</template>
<script>
  ...
  import lightbox from '../../../legacy/static/js/lightbox';

  export default {
  name: "AddToCartModal",
  components: {.....},
  mixins: [....., lightbox],
  ....
}
</script>

Additionally, here is the pertinent code excerpt from lightbox.js:

// Code has been omitted for brevity

Despite attempting to add

export default {showBasketHelpLightbox, closeBasketHelpLightbox}
at the end of lightbox.js and importing these function names as mixins in the Vue component, issues arose due to the presence of the word default in the file.

Answer №1

To open the lightbox, use a computed property like this: https://jsfiddle.net/L3fjdwuh/

// Example using lodash.js
new Vue({
  el: '#app',
  data: {
    arr: [1,2,3,4],
  },
  computed: {
    LODASH() {
        return _;
    },
  },
});

Update (from comments):

// Legacy static/js/lightbox file
export default {
  showBasketHelpLightbox,
};


// Inside your component
import { default as lightbox } from '...';

...
computed: {
  lightbox() {
    return lightbox;
  },
},
...

Answer №2

To incorporate it into your component, simply include it in the data properties:

import lightbox from '../../../legacy/static/js/lightbox';

export default {
  name: "ImageGalleryModal",
  components: {.....},
  data: () => ({
    lightbox,
  }),
}

Avoid using mixins unless it's specifically a vue mixin. By placing it in the data props, you can easily access it in your template.

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 the process for transforming a String into an HTML element within a Next JS Application?

I stored the product description as HTML elements in a Database, but when I try to render this data into a div, it displays as a String. I am looking to showcase all the data as HTML elements in my Next JS application. I attempted using JSON.parse, but unf ...

Ways to execute a JavaScript function upon a button click instead of during the page load completion

I searched on Google but couldn't find the answer, so I'm asking here for help. In the attached screenshot located at the end, when the user clicks on the download button, some backend actions are performed such as file transfer. Once the proces ...

Ways to verify the data type of a column in a table

I am currently working on making a table configurable. To achieve this, I am creating a demo component that will allow me to build my own customizable table. I intend to pass certain parameters to my table such as column names and data. The column names s ...

Transform a Vue.JS project into a Nuxt.JS project

Is it possible to transform a Vue.JS project into a Nuxt.JS project? Vue.js Project If you want to view the complete Vue.JS project, click here. This project utilizes the npm package vue-conversational-form to convert web forms into conversations using ...

Retrieve information in JSON structure

Currently, I am making a GET http request to the Google Civic's API. The purpose is to provide an address and in return receive information about members of the U.S. government based on the location of the provided address. // In the code below, the ...

Tips for modifying the value of a JSON object using Javascript or Jquery

I am looking to retrieve the value of a specific key, potentially accessing nested values as well. For example, changing the value of "key1" to "value100" or "key11" to "value111. { "key1": "value1", "key2": "value2", ...

Encountering a Selection Issue with IE8

I am encountering an issue with my script that involves a form with two select elements for region and state, allowing users to filter states based on the selected region. Everything works smoothly except for one problem - when testing in IE8, I receive th ...

I'm looking to customize the background color and font size for an accordion in Bootstrap 4 with Vue.js. Can anyone provide guidance

Hello, I am currently using an accordion with Bootstrap 4 and Vue.js. Here is the code snippet: <div class="faq_accordion" role="tablist"> <b-card no-body class="mb-1"> <b-card-header header-tag=" ...

React is unable to identify the `isDisabled` attribute on a DOM element within an img tag

After updating my React/Next.js App, I encountered the following issue: Upon investigation, React is indicating that the isDisabled prop is not recognized on a DOM element. To resolve this, you can either specify it as lowercase isdisabled if you want it ...

Error on homepage with Vue Storefront API issue

I have successfully implemented vue-storefront on . However, the standard homepage layout is not displaying correctly due to an error, most likely related to the API. There are no output errors in the terminal, all systems seem to be running smoothly wit ...

In Vuex, computed properties do not exhibit reactivity

I am attempting to create a shared state among several components, where each sub-component and parent component can update the shared property stored in vueX state. Here is a simplified "How to reproduce" example: Vue.component('urlQueryComponent&a ...

"Unlocking the potential of Framework7 + Vue to seamlessly integrate footers into panels

I am currently working on a website and looking to incorporate the Framework7 with Vue. The task at hand involves creating a side panel with a list inside it. One specific requirement is to add an exit icon at the end of this panel. Any assistance in ach ...

Handle Ajax requests to prevent multiple submissions upon clicking

Seeking a solution to avoid multiple requests when the user clicks on the login or register button. The code provided below is not functioning as expected; it works fine the first time but then returns false. $('#do-login').click(function(e) { ...

What is the best way to set wrapper props without hiding it from showing up in attrs?

Creating a Wrapper Component with Specific Props <script setup lang="ts"> import InputBase,{ Props as InputBaseProps } from "./InputBase.vue"; interface Props extends InputBaseProps { label?: string; labelClassName?: string; ...

Troubleshooting: Problems with AngularJS $http.get functionality not functioning as expected

I have a user list that I need to display. Each user has unread messages and has not created a meal list yet. I want to make two http.get requests within the main http.get request to retrieve the necessary information, but I am facing an issue with asynchr ...

Exploring the application of a counter flag within a v-for loop

Is it possible to use a counter flag within a nested v-for loop to keep track of the total number of iterations? Here is an example of my template: <a :href="'#/product/'+list.id" :id="ikeyCounter" v-for="item, ikey in section.list" class="mo ...

Utilize Vuex store in Vue without the need for import statements within components

When working with Vue 2/3 and in an ES6 environment, I often access a Vuex store in an external JS file using the following method: // example.js import { store } from '../store/index'; console.log(store.state.currentUser); While this method w ...

ReactJS instance.render function error: How to solve it?

I encountered a strange error message while working with reactjs, which states that instance.render is not a function in reactjs. Despite my efforts, I have been unable to identify the source of this error. Below are the contents of index.js and search_bar ...

tips for successfully transferring date and time data between json and nosql databases like firestore

Input: Created_At:Monday, 29 April 2019 15:07:59 GMT+05:30 Updated_At:Monday, 29 April 2019 15:07:59 GMT+05:30 I attempted to export data in JSON format from Firestore using the npm package firestore-export-import. However, the output I received was: ...

Creating a custom image component in React that is capable of rendering multiple images

Apologies for my poor English, as I am fairly new to React (just started learning yesterday). I am interested in learning how to render a variable number of images. For example, if I specify the number of images with an array containing file names, I want ...