gpt4 book ai didi

javascript - 捕获组件中底层元素的事件

转载 作者:搜寻专家 更新时间:2023-10-30 22:39:14 25 4
gpt4 key购买 nike

尝试使用 this组件。

<select2 v-model="value" :options="options" @change="onChange()"></select2>

@change 回调未被调用。我知道我可以使用 watch: { value: function () { ... } 但是,有没有办法捕获底层标记事件?

最佳答案

在当前版本中,select2 组件不处理on-change 函数。为此,您必须修改 select2 组件,您必须再添加一个 prop:onChange 并在组件内部执行传入此 prop 的函数,更改将类似于以下:

Vue.component('select2', {
props: ['options', 'value', 'onChange'], //Added one more prop
template: '#select2-template',
mounted: function () {
var vm = this
$(this.$el)
.val(this.value)
// init select2
.select2({ data: this.options })
// emit event on change.
.on('change', function () {
vm.$emit('input', this.value)

//New addition to handle onChange function
if (this.onChange !== undefined) {
this.onChange(this.value)
}
})
},
watch: {
value: function (value) {
// update value
$(this.$el).select2('val', value)
},
options: function (options) {
// update options
$(this.$el).select2({ data: options })
}
},
destroyed: function () {
$(this.$el).off().select2('destroy')
}
})

现在,您可以像下面这样传递一个将在 onChange 上执行的函数:

<select2 v-model="value" :options="options" :on-change="onChange()"></select2>

关于javascript - 捕获组件中底层元素的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41437120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com