Ich habe eine einfache App, die erfasst, was in einer Eingabe ist, und druckt es in einer h1.
<div id="app">
<div>
<module v-on:button-pressed="parentPrint(capturedInput)"></module>
</div>
<h1>{{ h1Text }}</h1>
</div>
Das Problem ist, das h1 wird nicht aktualisiert, um den Eingabewert widerzuspiegeln.
Vue.component('module', {
template:`
<div>
<input type="text" placeholder="Type Something Awesome" v-model="capturedInput" />
<button v-on:click="activateButtonPress">Change text</button>
</div>
`,
data(){
return {
capturedInput: ''
}
},
methods:{
activateButtonPress: function(){
console.log(this.capturedInput)
this.$emit('button-pressed', this.capturedInput)
}
}
})
new Vue({
el:'#app',
data:{
h1Text: 'Should reflect user input'
},
methods:{
parentPrint: function(capturedInput){
this.h1Text = capturedInput;
}
}
})
Wie übertrage ich Daten tatsächlich in die emit und habe sie im übergeordneten Bereich verfügbar? capturedInput
hält für mich undefined
zurück.
Wenn es hilft, [codepen] (https://codepen.io/modermo/pen/qPmzJW?editors=1010) hier – Modermo