Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Flickity Events? #45

Open
siberiadev opened this issue Nov 21, 2018 · 12 comments
Open

How to use Flickity Events? #45

siberiadev opened this issue Nov 21, 2018 · 12 comments

Comments

@siberiadev
Copy link

Could you tell me how to use events API of Flickity?

this.$refs.slider.on('select', function () {
      console.log(vm.$refs.slider.flickity.selectedIndex);
});

This code doesnt work.
I get the error "Cannot read property 'on' of undefined"

@nikwins
Copy link

nikwins commented Nov 21, 2018

Try

this.$refs.flickity.on('select', function () {
            console.log(vm.$refs.flickity.selectedIndex());
});

Otherwise just console.log this.$refs to see how your component is named.

This works for me with every event besides the "ready" Event. I don't see why it's not triggered, so if anybody has an idea, please let me know.

@lukepearce
Copy link

If I wrap the event listener in a timeout I don't get the error.

setTimeout(() => {
  this.$refs.flickity.on('change', (event) => {
    this.$emit('change', event);
  });
}, 1000);

@nikwins
Copy link

nikwins commented Nov 22, 2018

The ready event still doesn't get triggered for me

@siberiadev
Copy link
Author

siberiadev commented Nov 22, 2018 via email

@siberiadev
Copy link
Author

setTimeout(() => { this.$refs.flickity.on('change', (event) => { this.$emit('change', event); }); }, 1000);

YEAH!!! IT WORKS!! THANKS

@OriginalEXE
Copy link

Use the mounted hook to bind your events, and put it inside the this.$nextTick(() => { });, no need for the setTimeout with a random delay.

@lukepearce
Copy link

Use the mounted hook to bind your events, and put it inside the this.$nextTick(() => { });, no need for the setTimeout with a random delay.

I get TypeError: Cannot read property 'on' of undefined when I place in mounted using nextTick. If I console log this.$refs.flickity I can see the component.

@OriginalEXE
Copy link

That's weird. Could you create a reproduction on codesandbox? I would be interested in finding out what's causing it.

You are immediately rendering it inside a component, right?

@lukepearce
Copy link

lukepearce commented Jan 4, 2019

After starting to set this up on codesandbox I remembered that I adapted vue-flickity into my own component (to add a few things) and in doing so I'm actually using https://github.com/metafizzy/flickity-bg-lazyload instead of the standard flickity lib.

I expect it's either a change in flickity-bg-lazyload or perhaps something to do with a feature I added that allows me to delay the start of a carousel. Regardless of what it might be, it isn't on you to help me debug that ;)

Thanks for your suggestions so far and writing the component in the first place 🙌

@Yakoot
Copy link

Yakoot commented Feb 19, 2019

Use the mounted hook to bind your events, and put it inside the this.$nextTick(() => { });, no need for the setTimeout with a random delay.

I get TypeError: Cannot read property 'on' of undefined when I place in mounted using nextTick. If I console log this.$refs.flickity I can see the component.

<flickity ref="flickity"
                @init="onInit"...

and then you can bind your events in onInit method

@CodyEddings
Copy link

Is there a sample solution for event listening with Vue 3 & TypeScript?

@CodyEddings
Copy link

Solution for Vue 3 & Typescript event listening here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants