捕获 child 个组件发出的动作
Capture child components emitted action
我正在尝试对 child 组件发出的动作做出反应。 child 发出一个名为 init
的 event/trigger/action。我尝试在我的 parent 组件 events
地图中注册该事件。但是回调永远不会触发,这让我觉得我错误地注册了发出的动作。
我如何对 child 发出的 event/trigger/action 作出反应?
<template>
<flickity v-if="caseStudies && caseStudies.length > 0" id="case--studies_slider"ref="flickity" :options="flickityOptions">
<div v-for="cs in caseStudies">
{{cs.title}}
</div>
</flickity>
</template>
<script type="text/javascript">
import Flickity from 'vue-flickity'
// NOTE computed properties (caseStudies) has been removed for brevity
// but I confirm that it does load with more than 1 element in the array
export default {
components: {
'Flickity': Flickity, // Upon initialisation this should emit 'init'
},
data() {
return {
flickityOptions: {
prevNextButtons: false,
}
}
},
events: {
// This never fires!
init() {
console.log(foo)
this.$refs.flickity.on('dragStart', (ev, pointer) => {
$('#case--studies_slider').addClass('is-dragging')
})
this.$refs.flickity.on('dragEnd', (ev, pointer) => {
$('#case--studies_slider').removeClass('is-dragging')
})
}
},
mounted() {
// The below code causes a runtime error 'this.$refs.flickity' is undefined
this.$refs.flickity.on('dragStart', (ev, pointer) => {
$('#case--studies_slider').addClass('is-dragging')
})
this.$refs.flickity.on('dragEnd', (ev, pointer) => {
$('#case--studies_slider').removeClass('is-dragging')
})
}
}
</script>
您需要通过 v-on 将子事件绑定到父事件:
<template>
<flickity v-if="caseStudies && caseStudies.length > 0" id="case--
studies_slider"ref="flickity" :options="flickityOptions"
v-on:init="doSomething()">
<div v-for="cs in caseStudies">
{{cs.title}}
</div>
</flickity>
</template>
<script type="text/javascript">
import Flickity from 'vue-flickity'
export default {
components: {
'Flickity': Flickity,
},
data() {
return {
flickityOptions: {
prevNextButtons: false,
}
}
},
methods: {
doSomething() {
// this will be called when the 'init' event is triggered
}
},
}
</script>
我正在尝试对 child 组件发出的动作做出反应。 child 发出一个名为 init
的 event/trigger/action。我尝试在我的 parent 组件 events
地图中注册该事件。但是回调永远不会触发,这让我觉得我错误地注册了发出的动作。
我如何对 child 发出的 event/trigger/action 作出反应?
<template>
<flickity v-if="caseStudies && caseStudies.length > 0" id="case--studies_slider"ref="flickity" :options="flickityOptions">
<div v-for="cs in caseStudies">
{{cs.title}}
</div>
</flickity>
</template>
<script type="text/javascript">
import Flickity from 'vue-flickity'
// NOTE computed properties (caseStudies) has been removed for brevity
// but I confirm that it does load with more than 1 element in the array
export default {
components: {
'Flickity': Flickity, // Upon initialisation this should emit 'init'
},
data() {
return {
flickityOptions: {
prevNextButtons: false,
}
}
},
events: {
// This never fires!
init() {
console.log(foo)
this.$refs.flickity.on('dragStart', (ev, pointer) => {
$('#case--studies_slider').addClass('is-dragging')
})
this.$refs.flickity.on('dragEnd', (ev, pointer) => {
$('#case--studies_slider').removeClass('is-dragging')
})
}
},
mounted() {
// The below code causes a runtime error 'this.$refs.flickity' is undefined
this.$refs.flickity.on('dragStart', (ev, pointer) => {
$('#case--studies_slider').addClass('is-dragging')
})
this.$refs.flickity.on('dragEnd', (ev, pointer) => {
$('#case--studies_slider').removeClass('is-dragging')
})
}
}
</script>
您需要通过 v-on 将子事件绑定到父事件:
<template>
<flickity v-if="caseStudies && caseStudies.length > 0" id="case--
studies_slider"ref="flickity" :options="flickityOptions"
v-on:init="doSomething()">
<div v-for="cs in caseStudies">
{{cs.title}}
</div>
</flickity>
</template>
<script type="text/javascript">
import Flickity from 'vue-flickity'
export default {
components: {
'Flickity': Flickity,
},
data() {
return {
flickityOptions: {
prevNextButtons: false,
}
}
},
methods: {
doSomething() {
// this will be called when the 'init' event is triggered
}
},
}
</script>