vue.js - Vue Js中如何将动态函数名传递给点击事件
问题描述
有什么方法可以从参数中传递函数名吗?
像这样的东西..
<tr v-for="item in items" class="static"
v-bind:class="{'evenRow': item.oddeven=='1', 'oddRow': item.oddeven=='0' }"
@click="item.click(item.contactID)" >
</tr>
item.click 在渲染页面时没有转换为相应的函数。什么是正确的方法任何建议将不胜感激?
解决方案
要使用动态函数调用,建议有一个辅助函数来接收函数名称并调用相应的函数。
handle_function_call(function_name) {
this[function_name]()
},
从模板中迭代项目时,您可以通过传递函数名称来调用该函数,例如
<button v-for="button of items"
:key="button.id"
@click="handle_function_call(button.fn_name)" //=> note here
>
{{ button.text }}
</button>
在 jsfiddle中查看它的实际效果
推荐阅读
- sql-server-2017 - 到期余额利息的计算
- google-sheets - 自动计算单元格
- java - 特定模式作为模运算符的输出
- ajax - jquery 自动完成现在工作正常
- oauth-2.0 - 为授权重定向 URI 添加 http://192.168.64.2:8000 域
- python - 有没有办法阻止“unicode escape”错误无法解码字节
- python-3.x - 出现错误 {"detail":"Method \"GET\" not allowed."}
- c - 我想在 UNIX 中制作复制功能文件
- go - 如何在 Golang 中修复“net/http set header ORIGIN”
- sql-server - 如果表中不存在当前记录,如何获取上一个记录