25.06.2018 Views

Full-Stack Vue.js 2 and Laravel 5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Changing carousel images<br />

Returning to CarouselControl, let's respond to a user's click by using the v-on<br />

directive <strong>and</strong> triggering a method, clicked. This method will, in turn, emit a<br />

custom event, change-image, which will include a payload of either -1 or 1,<br />

depending on whether the state of the component is left or right.<br />

Just like with v-bind, there is a shorth<strong>and</strong> for v-on as well. Simply<br />

replace v-on: with @; for instance, is the<br />

equivalent of .<br />

resources/assets/<strong>js</strong>/app.<strong>js</strong>: components: { 'carousel-control': { template: ``, props: [ 'dir' ], computed: { classes() {<br />

return 'carousel-control fa fa-2x fa-chevron-' + this.dir; } }, methods: { clicked()<br />

{ this.$emit('change-image', this.dir === 'left' ? -1 : 1); } } } }<br />

Open <strong>Vue</strong> Devtools to the Events tab, <strong>and</strong>, at the same time, click on the<br />

carousel controls. Custom events are logged here, so we can verify change-image is<br />

being emitted:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!