69
rated 0 times
[
74]
[
5]
/ answers: 1 / hits: 6286
/ 6 Years ago, sat, december 8, 2018, 12:00:00
I have just started learning Vue.js and I am trying to get the values from my components. Is there a way to have multiple input fields in my component and get the value of each outside of the component?
For example, if I wanted to get and change the value for both of my input fields in my simple-input component. Right now they both have the same value but I would like to have 2 different values in each input.
My component
Vue.component('simple-input', {
template: `
<div>
<input type=text :value=value @input=$emit('input', $event.target.value)>
<input type=text :value=value @input=$emit('input', $event.target.value)>
</div>
`,
props: ['value']
});
main.js
new Vue({
el: '#root',
data: {
value1: 'Initial value 1',
value2: 'Initial value 2',
value3: 'Initial value 3'
},
methods: {
alertSimpleInput1() {
alert(this.value1);
this.value1 = 'new';
}
}
});
HTML
<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</button>
More From » vue.js