2017-09-05 11 views
2

제 질문은 Vuejs $emit doesn't fire on callback과 같습니다. 하지만 프로젝트에서 superagent를 사용했습니다.

//Parent.vue 
<Child v-on:savevideo="toSaveVideo"/> 
... 
methods:{ 
    toSaveVideo:function(data){ 
    console.log('add'); 
    } 
} 

//Child.vue 
<button @click="toAdd">Add</button> 
... 
methods:{ 
    toAdd:function(){ 
    ... 
    let self = this; 
    superagent 
     .get(url) 
     .query({data:data}) 
     .end(function(err,res){ 
     //trigger parent function 
     let resData = res.body.data; 
     self.$emit('savevideo',resData); 
    }) 
    } 
} 

요청이 성공하지만 부모의 트리거 'savevideo'방법 'toSaveVideo은'아무것도 인쇄되지 않은 경우 : 여기 내 코드입니다. 그러나 콜백 외부에 이미 트를 넣으면 모든 것이 정상입니다. $ emit 이벤트가 콜백에서 실행되지 않는 이유는 무엇입니까?

답변

0

괜찮 았어.

'v-if'가 하위 구성 요소에 바인딩됩니다.

내 프로젝트의 하위 구성 요소에이 하위 구성 요소를 닫기위한 또 다른 트리거 '닫기'가 있으며 콜백 외부에서 방출되어 이로 인해 문제가 발생했습니다.

//Parent.vue 
<Child v-on:savevideo="toSaveVideo" v-if="showChild" v-on:close="toClose"/> 
... 
methods:{ 
    toClose:function(){ 
    this.showChild = false; 
    } 
} 

//Child.vue 
<button @click="toAdd">Add</button> 
... 
methods:{ 
    toAdd:function(){ 
    ... 
    let self = this; 
    superagent 
     .get(url) 
     .query({data:data}) 
     .end(function(err,res){ 
     //trigger parent function 
     let resData = res.body.data; 
     self.$emit('savevideo',resData); 
     //'close' should be emitted here! 
    }) 
    this.$emit('close'); //bad code!! This cause the problem! 
    } 
}