admin管理员组文章数量:1794759
Vue中根据方法名称动态调用方法
很重要: 解释说明一下, this需要传入目标方法, 否则,在目标方法中修改data中的值, 将不会重新渲染dom, 跟v-if结合使用的过程中需要注意. 现象就是, 修改v-if绑定的值, 切换不生效.
// 解释说明一下, this需要传入目标方法, 否则,在目标方法中修改data中的值, 将不会重新渲染dom, 跟v-if结合使用的过程中需要注意 <script> export default { name: "DynamicFunc", data() { return { key: '', value: '', } }, mounted() { // 测试一下 this.callModelFun('funcA'); this.callModelFun('funcB'); }, methods: { test(func) { }, /** * 根据方法名称调用方法 */ callModelFun(funcName) { let methods = this.$options.methods; // 解释说明一下, this需要传入目标方法, 否则,在目标方法中修改data中的值, 将不会重新渲染dom, 跟v-if结合使用的过程中需要注意 const _this = this; methods[funcName](_this); }, /** * funcA * @param _this */ funcA(_this) { console.log("调用A方法") _this.key = "A"; _this.value = "A"; console.log(_this.key, _this.value) }, /** * funcB * @param _this */ funcB(_this) { console.log("调用B方法") _this.key = "B"; _this.value = "B"; console.log(_this.key, _this.value) } } } </script>版权声明:本文标题:Vue中根据方法名称动态调用方法 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686490900a73436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论