나는 효과적인 2 개의 바이올린을 보관하고 있습니다. 한 가지 방법은 v-on, emitter를 사용하여 하위 구성 요소와 상위 구성 요소 간의 통신을 아카이브하는 것입니다. 한 가지 방법은 하위 구성 요소에서 상위 데이터를 직접 업데이트하는 것입니다.
나는 첫번째 방법을 추천한다.
확인 첫 번째, 사용 V-에, 에미 터 방식, https://jsfiddle.net/seaify/p8fh108j/
Vue.component('text-input', {
props: ["parent_field"],
template: `
<input type="text" v-model="text">
`,
data: function() {
return {text: ''}
},
watch: {
text: function(value, old_value) {
this.$emit('update_parent', this.parent_field, value)
}
}
});
Vue.component('my-app-form', {
template: `
<div class="my-app-form">
<div class="my-text-input">
Name: <text-input parent_field="name" v-on:update_parent='update_self'></text-input>
</div>
<div class="my-text-input">
Email: <text-input parent_field="email" v-on:update_parent='update_self'></text-input>
</div>
<div class="result">Result: name = {{name}}, email = {{email}}</div>
</div>
`,
methods: {
update_self: function(key, value) {
this[key] = value
}
},
data: function() {
return {
name: "",
email: ""
}
},
});
new Vue({
el: '#app',
template: `
<div>
<my-app-form></my-app-form>
</div>
`,
});
확인이 바이올린 예를 들어, 직접 통신하지 않고, 부모 구성 요소를 업데이트, 마니에 https://jsfiddle.net/seaify/jczowb54/10
Vue.component('text-input', {
props: ["parent_field"],
template: `
<input type="text" v-model="text">
`,
data: function() {
return {text: ''}
},
watch: {
text: function(value, old_value) {
this.$parent[this.parent_field] = value
}
}
});
Vue.component('my-app-form', {
template: `
<div class="my-app-form">
<div class="my-text-input">
Name: <text-input parent_field="name"></text-input>
</div>
<div class="my-text-input">
Email: <text-input parent_field="email" ></text-input>
</div>
<div class="result">Result: name = {{name}}, email = {{email}}</div>
</div>
`,
data: function() {
return {
name: "",
email: ""
}
},
});
new Vue({
el: '#app',
template: `
<div>
<my-app-form></my-app-form>
</div>
`
});
감사합니다, 나는 당신의 바이올린을 포크하고 코드를 업데이트했다.
이'$ event'는 그 트릭을 할 것이며, 그것에 대해 알지 못했습니다. 감사. – cassioscabral