반응형
Vue.js2 - 상위 구성 요소에 의해 $emit 이벤트가 캡처되지 않음
나는 부에즈에서 부트스트랩 탭 구성 요소를 만들려고 한다.탭 구성 요소는 두 부분으로 구성된다.첫째, 부모tabs-list
여러 개의 구성 요소를 포함하는 구성 요소tab-list-item
구성 요소여기 둘 다에 대한 코드가 있어
//Vue component template for tabs list.
Vue.component('tabs-list', {
template: `<ul class="nav nav-tabs nav-justified" role="tablist">
<tab-list-item v-for="concept in concepts" :key="concept.id" :concept="concept" :selected="concept.active">{{ concept.title }}</tab-list-item>
</ul>`,
data() {
return {
activeTab: 1,
concepts: [ { title: 'Tab A', id:1, active: true},
{ title: 'Tab B', id:2, active: false},
{ title: 'Tab C', id:3, active: false},
{ title: 'Tab D', id:4, active: false},
{ title: 'Tab E', id:5, active: false},
{ title: 'Tab F', id:6, active: false},
{ title: 'Tab G', id:7, active: false},
{ title: 'Tab H', id:8, active: false}]
}
},
methods: {
tabItemClicked(concept) {
console.log(concept);
this.activeTab = concept.id;
this.concepts.forEach(tab=> {
tab.active = (tab.id === concept.id);
});
}
}
});
//Vue component template for tab list item.
Vue.component('tab-list-item', {
props: ['concept', 'selected'],
template: `<li role="presentation" :class="{active:concept.active}">
<a :href='computedHref' :aria-controls="ariaControls" role="tab" data-toggle="tab" @click="tabClicked">
<img :src="aquaImage" class="image-responsive concept-image img-active">
<slot></slot>
</a>
</li>`,
computed: {
computedHref: function() {
return "#concept"+this.concept.title
},
ariaControls: function() {
return "concept"+this.concept.title
},
aquaImage: function() {
return "/images/"+this.concept.title+"-aqua.png"
}
},
data() {
return {
isActive: false
}
},
mounted() {
this.isActive = this.selected;
},
methods: {
tabClicked: function() {
this.$emit('tabItemClicked', [this.concept]);
}
}
});
그래서, 여기 나의tab-list-item
어떤 사건을 내보내야 한다.tabItemClicked
탭 중 하나를 클릭할 때그러나 콘솔에 아무것도 기록되지 않고 있다.내가 Vue 개발자 콘솔을 보면, 나는 그 이벤트가 방출되는 것을 본다.그런데 왜 부모에게 잡혀가지 않는 걸까?tabs-list
방법은?어떤 도움이라도 크게 고마워할 것이다!
상위 구성 요소 템플릿에서 이벤트를 명시적으로 청취해야 함
Vue.component('tabs-list', {
template: `<ul class="nav nav-tabs nav-justified" role="tablist">
<tab-list-item v-on:tabItemClicked="tabItemClicked" v-for="concept in concepts" :key="concept.id" :concept="concept" :selected="concept.active">{{ concept.title }}</tab-list-item>
</ul>`,
//....,
methods: {
tabItemClicked(concept) {
console.log(concept);
this.activeTab = concept.id;
this.concepts.forEach(tab=> {
tab.active = (tab.id === concept.id);
});
}
}
}
낙타 케이싱된 사용자 지정 이벤트는 상위에서 트리거를 호출하지 않는다.변화하다this.$emit('tabItemClicked', [this.concept]);
로this.$emit('tab_item_clicked', [this.concept]);
https://github.com/vuejs/vue/issues/4044을 참조하십시오.
반응형
'programing' 카테고리의 다른 글
vuejs 차트j에 json 데이터 표시 (0) | 2022.04.12 |
---|---|
계산된 속성에서 예기치 않은 부작용을 방지하는 방법 - VueJs (0) | 2022.04.12 |
VueJ에서 중첩된 자식 구성 요소에 액세스s (0) | 2022.04.11 |
HTML 입력에서 vue.js 데이터로 기본값 로드 (0) | 2022.04.11 |
필요한 조언:익스프레스 기능이 있는 VueJS 풀스택 앱 - 백엔드(서버)에서 2개의 스토어, 별도의 Vue 앱 또는 관리자용 익스프레스 app를 사용해야 함 (0) | 2022.04.11 |