Troubleshooting custom buttons error when using carousel in Nuxt app: "Flickity is not defined"

I am attempting to connect my own personalized buttons to a flickity carousel within a nuxt application. Within my carousel component, the following code is present:

<template>
    <ClientOnly>
        <Flickity
            ref="flickity"
            :key="keyIncrementer"
            class="carousel"
            :class="{ 'carousel--active': active }"
            :options="computedOptions"
        >
            <slot />
        </Flickity>
    </ClientOnly>
</template>

<script>
export default {
  beforeCreate() {
      var flkty = new Flickity(".carousel");
      
      var previousButton = document.querySelector('.button--previous')
      previousButton.addEventListener('click', function () {
          flkty.previous()
      })
      
      var nextButton = document.querySelector('.button--next')
      nextButton.addEventListener('click', function () {
          flkty.next()
      })
  },
  beforeDestroy() {
      window.removeEventListener('resize', this.debounceRefresh)
      nextButton.removeEventListener('click', function () {
          flkty.next()
      })
      previousButton.removeEventListener('click', function () {
          flkty.previous()
      })
  },
}
</script>

Inside my plugins folder resides a vue-flickity.js file:

import Vue from 'vue'
import Flickity from 'vue-flickity'

Vue.component('Flickity', Flickity)

The carousel functions properly without utilizing my custom buttons. However, when using the customized buttons, an error message appears stating

ReferenceError: Flickity is not defined
for the line =>
var flkty = new Flickity(".carousel");
, along with Missing stack frames. Something seems amiss here, but what could it be?

Answer №1

It seems like there is a misunderstanding with your usage of Flickity within the new Flickity() statement. It is assumed that the Flickity constructor (from the flickity package) is globally defined, possibly by vue-flickity, but that may not be the case. Additionally, using both flickity and vue-flickity simultaneously in this manner is not intended.

If you want to utilize vue-flickity's next/previous methods as handlers for your own buttons, you should link them with @click handlers on the buttons that trigger this.$refs.flickity.next() and this.$refs.flickity.previous():

<Flickity ref="flickity" class="carousel">...</Flickity>
<button @click="next">Next</button>
<button @click="prev">Prev</button>
export default {
  methods: {
    next() {
      this.$refs.flickity.next()
    },
    prev() {
      this.$refs.flickity.previous()
    },
  },
}

This example mirrors the one shown in the vue-flickity documentation.

Check out the demo for more information.

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

issue arises when trying to build vue3 with element-ui on node:16-buster-slim container

dockerfile: FROM node:16-buster-slim RUN apt-get update WORKDIR /app COPY package.json /home RUN npm install --prefix /home package.json { "name": "test", "version": "0.1.0", "private": false, ...

Different types of templates exist in Vue.js

While setting up vue.js using CLI, I came across the command: vue init template-name project-name This got me thinking about the different templates available for Vue.js. How many templates are there and which ones would be best suited for my needs? ...

How can Vue-toastr and Vue2 be configured with global options for toastr notifications?

I'm currently utilizing vue-toastr in my project, which combines Laravel 5.5 and Vuejs 2. Although the toasts are displaying correctly, I am struggling to set global options for them, such as position... I have attempted to do this but have been uns ...

Possible solution to address the issue: xhr.js:178 encountered a 403 error when attempting to access https://www.googleapis.com/youtube/v3/search?q=Tesla

Encountering this console error: xhr.js:178 GET https://www.googleapis.com/youtube/v3/search?q=river 403 A specific component was designed to utilize the API at a later point: const KEY = "mykeyas23d2sdffa12sasd12dfasdfasdfasdf"; export default ...

What is the best way to integrate nano.uuid into a series of promises that are fetching data from the database?

When working with express routing and nano, I encountered a challenge: router.get('/', function (request, response) { db.view('designdoc', 'bydate', { 'descending': true }) .then(results => { // data ...

Fastify Schema Failing to Validate Incoming Requests

Currently, our backend setup involves using Node.js and the Fastify framework. We have implemented a schema in satisfy to validate user input. Below is the schema defined in schema.ts: export const profileSchema = { type: 'object', properti ...

Asynchronous loading of HTML templates using JQuery/JavaScript

Currently, I am in the process of creating a webpage that includes code snippets loaded from txt files. The paths and locations of these txt files are stored in a json file. First, the json file is loaded in, and it looks something like this: [ {"root":"n ...

Steps to successfully click a button once the popup window has finished loading entirely

Is there a way to programmatically click on an HTML element? I am currently using JQuery selectors to identify a button and then trigger a click event. $('span.firstBtn').click(); Once this button is clicked, a popup window appears. How can I w ...

What is the best way to retrieve a JSON element obtained from a Parse.com query?

I'm encountering difficulties when attempting to access a specific JSON element that I receive in response from a query made into a Parse.com Class. Despite reading through various questions and answers on similar topics, I have yet to find a solutio ...

Utilizing the Fetch API to retrieve a Flickr feed in JSON structure

When interacting with the flicker feed API, I have successfully received a JSON response using $.getJSON. However, when attempting to use Fetch instead, only an XML response seems to be retrieved. Example of working with $.getJSON: var flickerAPI = "http ...

When a v-list-group item is selected, remove the active class from the corresponding v-list-item

While using the Vuetify v-list-item directive (outside) along with other v-list-items inside a v-list-group for routing pages of a website, I am facing an issue: The routing is functioning properly, but every time I click on one of the submenus, the index ...

Making changes to the DOM within the mounted method will not be reflected in the JEST

Within a Vue component, the mounted method contains the following code: this.el = d3.select(this.$el); this.svg = this.el.select('svg') .attr('width', mainSvgPos.svgWidth) .attr('height', mainSvgPos.svgHeight) ...

Unable to modify document value in MongoDB using Node.js

Currently, I am attempting to fetch the value of a field form that is being sent to a subroute and utilize it to update a database collection. My ability to retrieve the form value and manipulate it is working fine; however, I encounter an issue when I sol ...

Attempting to retrieve information from JSON or JSONP through the utilization of the WOT API

Recently, I utilized the WOT (web of trust) API and encountered a response structured like this: process( { "www.google.com": { "target": "google.com", "0": [ 95, 84 ], "1": [ 95, 84 ], "2": [ 95, 84 ], "4" ...

What are the capabilities of an INNER JOIN query in Objection JS?

Here is the data model that I am working with: https://i.stack.imgur.com/g1T5i.jpg In this model, a User can be associated with multiple Projects, and a Project can have many Users. These relationships are managed through a join table called UserProjects. ...

Leverage Async/Await in React.js with the Axios Library

Recently, I came across an interesting article on Medium titled How to use async/await with axios in react The article discussed making a simple GET request to a server using Async/Await in a React.js App. The server returned a JSON object at /data with t ...

While continuing to input text, make sure to highlight a specific element from the search results list

Currently, I am using a customized search feature in my React.js application. I am looking for a way to focus on an element within the search results so that I can navigate using arrow keys. At the same time, I need to keep the input field focused to conti ...

experiencing unexpected outcomes while testing composable functions with fetch API

There is a composable function called useFetchData used to retrieve data: export const useFetchData = (q?: string) => { const data: Ref<Data | undefined> = ref(); const error: Ref<Error | undefined> = ref(); const isLoading = ref(true) ...

What is the correct way to include a variable such as /variable/ in a MongoDB query?

I need help passing in a searchTerm from a variable, what is the best way to do this? const mongoquery = { description: { $in: [ /searchTerm/ ] } }; I attempted it like this initially: const mongoquery = { description: { $in: [ `/${searchTerm}/` ] } }; H ...

How come the callback in Jquery fadeOut keeps looping repeatedly, and what can I do to stop this from happening?

My approach involves fading out a div box and implementing a callback function as shown below: function closeWindow(windowIdPrefix, speed) { $("#" + windowIdPrefix + "_ViewPanel").fadeOut(speed, function() { resetWindow(windowIdPre ...