admin管理员组文章数量:1291134
I just started learning Vue.js but I ran into a problem where I can't figure out how I could access and change a text field value that is inside my ponent.
Let's say I would like to access or change the value of my first input field that is inside my ponent
My ponent
Vueponent('simple-input',
{
template: `
<input type="text" value="Some value...">
`,
});
HTML
<div id="root">
<simple-input></simple-input>
<simple-input></simple-input>
<simple-input></simple-input>
<div @click="alertSimpleInput1">Show first input value</div>
<div @click="changeInput1('new value')">Change input value</div>
<div @click="alertSimpleInput2">Show second input value</div>
</div>
main.js
new Vue({
el: '#root',
});
I just started learning Vue.js but I ran into a problem where I can't figure out how I could access and change a text field value that is inside my ponent.
Let's say I would like to access or change the value of my first input field that is inside my ponent
My ponent
Vue.ponent('simple-input',
{
template: `
<input type="text" value="Some value...">
`,
});
HTML
<div id="root">
<simple-input></simple-input>
<simple-input></simple-input>
<simple-input></simple-input>
<div @click="alertSimpleInput1">Show first input value</div>
<div @click="changeInput1('new value')">Change input value</div>
<div @click="alertSimpleInput2">Show second input value</div>
</div>
main.js
new Vue({
el: '#root',
});
Share
Improve this question
asked Dec 7, 2018 at 9:04
LigaLiga
3,4396 gold badges38 silver badges67 bronze badges
1
- You can use v-model to achieve exactly what you want. see here – Andrew1325 Commented Dec 7, 2018 at 9:18
2 Answers
Reset to default 5Having value="Some value..."
in your template means that the input's value will be set to the string "Some value..." initially. You need to bind the input's value to a data property on the ponent. Use v-model
for a two-way binding (when the input value changes, it will update the value of the data property and vice versa).
In your example there's actually a bit more involved though since you want to obtain the input's value from the root ponent, therefore the <simple-input>
ponent must expose this; the way to do this is by using props (for parent-to-child data flow) and events (for child-to-parent data flow).
Untested:
Vue.ponent('simple-input', {
template: `
<input type="text" :value="value" @input="$emit('input', $event.target.value)">
`,
props: ['value'],
});
<div id="root">
<simple-input v-model="value1"></simple-input>
<simple-input v-model="value2"></simple-input>
<simple-input v-model="value3"></simple-input>
<button @click="alertSimpleInput1">Show first input value</button>
<button @click="changeInput1('new value')">Change input value</button>
<button @click="alertSimpleInput2">Show second input value</button>
</div>
new Vue({
el: '#root',
data: {
value1: 'Initial value 1',
value2: 'Initial value 2',
value3: 'Initial value 3',
},
methods: {
alertSimpleInput1() {
alert(this.value1);
},
alertSimpleInput2() {
alert(this.value2);
},
changeInput1(newValue) {
this.value1 = newValue;
},
},
});
I understand you're just learning Vue, but there's a lot to unpack here for a beginner. I won't go into detail because there's lots of information about these concepts already.
Read the following:
- Component Basics
- Using
v-model
on Components (for an explanation of the<simple-input>
code)
you can use $emit method for this purpose.
<simple-input @clicked="inputValue" :name="name"></simple-input>
parent methods
export default {
data: function () {
return {
name:null
}
},
methods: {
inputValue (value) {
console.log(value) // get input value
}
}
}
Vue.ponent('simple-input', {
data: function () {
return {
// name:null
}
},
watch:{
'name':function(){
this.$emit('clicked', this.name)
}
template: '<input type="text" v-model="name">'
props:['name']
})
本文标签: javascriptVuejsHow to get and set a value of a component input fieldStack Overflow
版权声明:本文标题:javascript - Vue.js - How to get and set a value of a component input field? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741499129a2381975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论