admin管理员组文章数量:1334342
I have a input with attribute "ref" and I don't want to use v-model
<div class="form-group m-b-40">
<input type="text" class="form-control" id="name" ref="name" required>
</div>
{{showInput}}
I want to show my input value automatically. I do this
methods: {
showInput: function () {
this.$refs.name.value
},
}
but it isn't updated.
I have a input with attribute "ref" and I don't want to use v-model
<div class="form-group m-b-40">
<input type="text" class="form-control" id="name" ref="name" required>
</div>
{{showInput}}
I want to show my input value automatically. I do this
methods: {
showInput: function () {
this.$refs.name.value
},
}
but it isn't updated.
Share Improve this question edited Apr 25, 2018 at 16:56 Hitesh Kansagara 3,5263 gold badges19 silver badges33 bronze badges asked Apr 25, 2018 at 16:52 MohammadJavad AbbasiMohammadJavad Abbasi 1772 gold badges3 silver badges12 bronze badges 3- "I don't want to use v-model" why? What are you actually trying to do? – zero298 Commented Apr 25, 2018 at 17:02
- @zero298 At first I get value from api. then user can edit this value. before save it i must show this changed value – MohammadJavad Abbasi Commented Apr 25, 2018 at 17:06
- 1 I don't understand why that matters. Please explain exactly what you are trying to do, what you have tried, and what isn't working. Provide as much context as you can. The MCVE page will probably help you understand why. – zero298 Commented Apr 25, 2018 at 17:08
2 Answers
Reset to default 3Because the value
of a ref
isn't an observable object unless it's bound to the ponent instance:
data() {
return {
name: ''
}
}
Then give your input
a :value="name"
and now it has an observer attached to it
I can't understand what you want to do,but the way you are doing it seems to be wrong.Anyway,you said i dont want to use v-model
.
I am going to show you how to do it without v-model,you can fetch the input value from api(you have to write your own code for this) and set it to input:
<template>
<div>
<div class="form-group m-b-40">
<input type="text" :value="text" @input="updateValue">
<hr>
</div>
The input value is: {{text}}
</div>
</template>
<script>
export default {
data() {
return {
text: ''
}
},
created() {
this.fetchFromApi()
},
methods: {
updateValue(value) {
let newValue = value.target.value
this.text = newValue
},
fetchFromApi() {
//write the code to get from API the input value and then:
this.text = 'input value' //set the input value
}
}
}
</script>
See it in action here
本文标签: javascriptHow to use refs in templatevuejsStack Overflow
版权声明:本文标题:javascript - How to use $refs in template - vuejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742368728a2461798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论