0
기본 Vue2 구성 요소가 있고 $emit
을 사용하여 하위 구성 요소에서 부모 구성 요소로 데이터를 전달하려고합니다. 참고 : my-component에는 행을 클릭하면 onRowClick이 성공적으로 실행되어 콘솔에 'foobar'를 출력하는 테이블이 있습니다. 어떤 이유로, $ emit에 부모 메소드가 실행되지 않고 성공이 콘솔에 기록되지 않습니다. 왜 그런지 아십니까?Vue - 자식에서 부모로 데이터 전달 - 기본 방사가 작동하지 않습니다.
import Vue from 'vue';
import MyComponent from "./components/MyComponent.vue";
window.onload = function() {
var main = new Vue({
el: '#app-v2',
components: { MyComponent },
methods: {
onCouponApplied(data) {
console.log("Success, coupon applied, " + data);
this.saying = "Sunglasses are " + data; //Sunglasses are cool
}
},
data: {
contactEditPage: {
saying: ""
}
}
});
}
MyComponent.vue
export default {
methods: {
onRowClick(event){
this.$emit('applied', 'Cool');
console.log("foobar");
}
HTML
<div id="app-v2">
<my-component @applied="onCouponApplied"></my-component>
{{ saying }} //trying to output: sunglasses are cool
</div>
아마도'$ emit'이 (가) 부모 구성 요소를 찾을 수 없습니다. 코드에서'window.onload' 래퍼를 제거해보십시오. –
@AnchovyLegend, 여기 Plunker (http://plnkr.co/edit/yldNSHD5S55VykT2FrRn?p=preview)가 코드 조각을 가지고 있습니다. 예제는 게시 된 바깥에 있어야합니다. – miqid