Vue-carousel: Feature request: emit event when currentPage changes

Created on 2 Mar 2017  路  6Comments  路  Source: SSENSE/vue-carousel

related: #25

I want to handle navigation from outside the Carousel. I am using Vuex, so it would be handy to have an event emitted when the currentPage changes. I could do it, add a watcher on currentPage that emits an event, and create a pull request, if you want.

I tried this, but it does not work, would be nice though:

watch: {
  '$refs.carousel.currentPage' () {
    if (this.currentPage !== this.$refs.carousel.currentPage) {
      store.dispatch('carousel/currentPageChanged', this.$refs.carousel.currentPage)
    }
  }
}

The rest should work like this (did not test):

computed: {
  ...mapGetters('carousel', [
    'currentPage'
  ])
},
watch: {
  currentPage () {
    if (this.currentPage !== this.$refs.carousel.currentPage) {
      this.$refs.carousel.goToPage(this.currentPage)
    }
  }
}

Thank you!

feature help wanted

All 6 comments

Hi @benjamindedonder,

Were you thinking of using Vue's built in events for this? Perhaps we can emit namespaced events like VueCarousel/pageChange on vm or this.$root whenever goToPage is called. I'd love to see a PR with an implementation of something like this!

Cheers 馃憤

I created two pull requests with two possible implementations;
by emitting a local (#29) or a global (#30) event.

I personally prefer a local event.

I merged in your change and released it under 0.6.4. Your contribution is appreciated! 馃

Hello,

I didn't find out how to use this "Event on page changed" feature properly. Through Vuex $store then ? . Am I wrong or is this not documented yet ? Can you please provide a very quick example, for example an implementation of a listener to that event ? That is just the little last thing I need.

Thanks.

@mandawan doesn't look like it is in the docs yet, here is how it works

<carousel ref='carousel' @pageChange='carouselPageChange'>

This will call the method carouselPageChange on the parent

    methods: {
      carouselPageChange: function () {
        console.log(this.$refs.carousel.currentPage)
      },

Yes, an example will be nice. How do you listen to that even on a global component? EJ:

import Vue from 'vue';
import VueCarousel from 'vue-carousel'; 

Vue.use(VueCarousel);

Thanks

Was this page helpful?
0 / 5 - 0 ratings