如果引用值有括号字符,如何访问 vuejs 引用 [英] how to access vuejs refs if ref value has bracket character

查看:30
本文介绍了如果引用值有括号字符,如何访问 vuejs 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问输入的引用,但仍然未定义.

I would like to access the ref of input, but still getting undefined.

//模板中的样本模板:

// sample in template template:

<input type="text" class="form-control form-control-sm" v-bind:value=meminfo.last_name name="online_membership[last_name]" ref="online_membership[last_name]" >

脚本:

methods() {
   function() {
     // i would like to access the input value of this ref
     this.$refs.online_membership[last_name]
   }
}

推荐答案

你应该像下面这样修改:

you should change something like below:

最佳实践:

//模板模板中的示例:

// sample in template template:

<input type="text" class="form-control form-control-sm" v-bind:value=meminfo.last_name name="online_membership[last_name]" :ref="online_membership[last_name]" >

脚本:

methods() {
   function() {
     // i would like to access the input value of this ref
     this.$refs[online_membership[last_name]]
   }
}

但是如果您想用作包含 '[' 的字符串,那么您可以使用以下解决方案:

but still if you want to use as a string including '[' then simply you can use below solution:

//模板模板中的示例:

// sample in template template:

<input type="text" class="form-control form-control-sm" v-bind:value=meminfo.last_name name="online_membership[last_name]" ref="'online_membership[last_name]'" >

脚本:

methods() {
   function() {
     // i would like to access the input value of this ref
     this.$refs['online_membership[last_name]']
   }
}

希望它能解决您的问题

这篇关于如果引用值有括号字符,如何访问 vuejs 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆