gpt4 book ai didi

javascript - VueJs 动态 v-on 事件可能吗?

转载 作者:行者123 更新时间:2023-11-30 07:54:08 25 4
gpt4 key购买 nike

是否可以在 VueJS 中设置动态事件?我尝试构建一个动态表单作为具有可以监听所有内容的输入的组件。这里有一个例子:

import Vue from 'vue';

let formItems = {
{type: 'checkbox', id: 'some-id', on:'change', model: 'someId'},
{type: 'url', id: 'another-id', on:'keyup', model:'anotherId'},
};

let params = {
someId: true,
anotherId: 'http://www.example.com',
};

new Vue({
el: '#app',
data: {
formItems: formItems,
params: params,
},
methods: {
checkInputParams(e) {
e.preventDefault();
// Do some stuff.
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.js"></script>

<div id="app">
<div class="form-group" v-for="item in formItems">
<input type="checkbox" <!-- Also need a workaround for dynamic type and v-model -->
:id="item.id"
:class="(item.class ? item.class : '')"
:title="(item.title ? item.title : '')"
:placeholder="(item.placeholder ? item.placeholder : '')"
:autocomplete="(item.autocomplete ? item.autocomplete : false)"
:disabled="(item.disabled ? item.disabled : false)"
:max="(item.max ? item.max : '')"
:min="(item.min ? item.min : '')"
:maxlength="(item.maxLength ? item.maxLength : '')"
:multiple="(item.multiple ? item.multiple : '')"
:name="(item.name ? item.name : '')"
:readonly="(item.readonly ? item.readonly : false)"
:required="(item.required ? item.required : false)"
v-on="{{ item.on }}:checkInputParams" <!-- Here I try the dynamic v-on -->
v-model="params[item.model]"/>
</div>
</div>

是否可以设置动态 v-on 事件,如 v-on="<variable>:<function>"

最佳答案

从 Vue 2.4.0+ 开始,v-on 接受对象语法

引用this documentation

关于javascript - VueJs 动态 v-on 事件可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44776751/

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