I have parent ponent vue like this :
<template>
<form>
<div class="row">
<div class="col-md-4">
<form-select id="color" name="color" :data="color">Color</form-select>
</div>
<div class="col-md-4">
<form-select id="size" name="size" :data="size">Size</form-select>
</div>
</div>
...
<a href="javascript:" class="btn btn-danger" @click="add">
Add
</a>
</form>
</template>
<script>
import FormSelect from '../form/FormSelect.vue'
export default {
data(){
return {
quantity: [
{id: 1, value: '1'},
{id: 2, value: '2'},
{id: 3, value: '3'}
],
size: [
{id: 1, value: 'S'},
{id: 2, value: 'M'},
{id: 3, value: 'L'}
]
}
},
ponents: {FormSelect},
methods: {
add(event) {
const color = document.getElementById('color').value,
size = document.getElementById('size').value
}
}
}
</script>
I have child ponent vue like this :
<template>
<div class="form-group">
<label :for="id" class="control-label"></label>
<select :id="id" :name="name" class="form-control" v-model="selected">
<option v-for="item in data">{{item.value}}</option>
</select>
</div>
</template>
<script>
export default {
props: ['id', 'name', 'data'],
data(){
return {
selected: ''
}
}
}
</script>
If I click add button, I success get values selected. But it still using javascript (document.getElementById)
I want to change it. So I want to using data binding vue ponent. But i'm still confused to use it
How can I do it with data binding?
I have parent ponent vue like this :
<template>
<form>
<div class="row">
<div class="col-md-4">
<form-select id="color" name="color" :data="color">Color</form-select>
</div>
<div class="col-md-4">
<form-select id="size" name="size" :data="size">Size</form-select>
</div>
</div>
...
<a href="javascript:" class="btn btn-danger" @click="add">
Add
</a>
</form>
</template>
<script>
import FormSelect from '../form/FormSelect.vue'
export default {
data(){
return {
quantity: [
{id: 1, value: '1'},
{id: 2, value: '2'},
{id: 3, value: '3'}
],
size: [
{id: 1, value: 'S'},
{id: 2, value: 'M'},
{id: 3, value: 'L'}
]
}
},
ponents: {FormSelect},
methods: {
add(event) {
const color = document.getElementById('color').value,
size = document.getElementById('size').value
}
}
}
</script>
I have child ponent vue like this :
<template>
<div class="form-group">
<label :for="id" class="control-label"></label>
<select :id="id" :name="name" class="form-control" v-model="selected">
<option v-for="item in data">{{item.value}}</option>
</select>
</div>
</template>
<script>
export default {
props: ['id', 'name', 'data'],
data(){
return {
selected: ''
}
}
}
</script>
If I click add button, I success get values selected. But it still using javascript (document.getElementById)
I want to change it. So I want to using data binding vue ponent. But i'm still confused to use it
How can I do it with data binding?
Share Improve this question asked Feb 20, 2018 at 4:07 moses tohmoses toh 13.2k81 gold badges265 silver badges459 bronze badges2 Answers
Reset to default 1You need to emit the event from child ponent to send your data and use the on method to get that data in the parent ponent:
Parent:
<form-select id="color" name="color" :data="color" v-on:triggerChange="changeColor">Color</form-select>
methods: {
// ...
changeColor(selected) {
// do what you want to do with selected
}
// ...
}
Child:
<select :id="id" :name="name" class="form-control" v-model="selected" v-on:change="applyColor">
methods: {
// ...
applyColor() {
this.$emit('triggerChange',this.selected);
}
// ...
}
As per your ment, you can do like this:
this.$parent.$options.methods.someParentMethod(data)
Note:
Use $parent and $children sparingly - they mostly serve as an escape-hatch. Prefer using props and events for parent-child munication.
Everything seems to be correct when you develop new things. Above answer is totally correct and appreciate the answer provided on time.
Posting this answer to describe the answer in more details. While developing application in Vue, you must have to understand few things like.
A. Communication between parent and child ponent
B. Non Parent-Child Communication
A. Communication between parent and child ponent
- Lets understand the munication between parent and child ponent. I have broke down into steps are few things to keep in mind
i) Bind some method X with parent so that method can listen emitted message by child
ii) Add props property in child ponent to bind data in child ponent
iii) this.$emit the same message (X) that is bind in parent ponent.
Parent ponent
<template>
<form>
<div class="row">
<div class="col-md-4">
<form-select id="color" name="color" (dropdownChanged)= 'colorChanged' :data="color">Color</form-select>
</div>
<div class="col-md-4">
<form-select id="size" name="size" :data="size" (dropdownChanged)= 'sizeChanged'>Size</form-select>
</div>
</div>
...
<a href="javascript:" class="btn btn-danger" @click="add">
Add
</a>
</form>
</template>
<script>
import FormSelect from '../form/FormSelect.vue'
export default {
data(){
return {
quantity: [
{id: 1, value: '1'},
{id: 2, value: '2'},
{id: 3, value: '3'}
],
size: [
{id: 1, value: 'S'},
{id: 2, value: 'M'},
{id: 3, value: 'L'}
]
}
},
ponents: {FormSelect},
methods: {
add(event) {
const color = document.getElementById('color').value,
size = document.getElementById('size').value
},
colorChanged(color) {
console.log('Color Changed', color);
},
sizeChanged(size) {
console.log('size Changed', color);
},
}
}
</script>
Child Component
<template>
<div class="form-group">
<label :for="id" class="control-label"></label>
<select (change)='dropdownChanged' :id="id" :name="name" class="form-control" v-model="selected">
<option v-for="item in data">{{item.value}}</option>
</select>
</div>
</template>
<script>
export default {
props: ['id', 'name', 'data'],
data(){
return {
selected: ''
}
},
methods: {
dropdownChanged() {
this.$emit('changesOccured',this.selected)
}
}
}
</script>
B. Non Parent-Child Communication
Sometimes two ponents may need to municate with one-another but they are not parent/child to each other. In simple scenarios, you can use an empty Vue instance as a central event bus:
var bus = new Vue()
// in ponent A's method
bus.$emit('id-selected', 1)
// in ponent B's created hook
bus.$on('id-selected', function (id) {
// ...
})
Reference - https://v2.vuejs/v2/guide/ponents.html
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745247625a4618484.html
评论列表(0条)