【发布时间】:2025-11-23 10:20:14
【问题描述】:
我有一个步骤组件,当我单击下一步按钮时,我需要在我的步骤组件中触发一个事件。
这个事件应该被另一个组件接收,它代表当前步骤中页面的内容。
这是我尝试过的:
步骤组件模板:
<template>
<div class="steps">
<div class="steps-content">
<section class="steps-panel" v-for="(stepPage, index) in steps">
<header class="posA wrap pTs">{{$t(title)}}</header>
<component :is="stepPage">
<!-- Summary component is injected here -->
</component>
</section>
</div>
<div role="navigation">
<ol class="fixed nav nav--block steps-nav w100">
<li class="steps-label txtCap" v-for="(stepPage, index) in steps">
{{$t(stepPage.name)}}
</li>
</ol>
<button class="steps-button" type="button">
<i class="pf pf-arrow-left"></i>
<span class="steps-button-label">{{$tc('previous')}}</span>
</button>
<button class="steps-button" type="button" v-on:click="next">
<span class="steps-button-label">{{$tc('next')}}</span>
<i class="pf pf-arrow-right"></i>
</button>
</div>
</div>
</template>
Steps组件方法:
methods: {
next(event) {
console.log('emit stepnext')
this.$emit('stepnext')
}
}
我在步骤模板中使用v-on:click="next" 调用它(在“下一步”按钮上)
从console.log看到执行了click事件,$emit调用没有触发任何错误,所以此时似乎可以正常工作了。
摘要组件
摘要组件是“步骤”中的组件之一,由步骤模板中的此条目加载:
<component :is="stepPage"></component>
在知道单击此按钮时该做什么的摘要组件中,我尝试通过在模板中包含此事件来获取此事件:
<div class="wrap" v-on:stepnext="stepfinish">
... content ...
</div>
... 以及一个名为 stepfinish 的摘要组件中的方法执行该操作,但发出的事件似乎永远不会到达我的摘要组件。
我该如何解决这个问题?
【问题讨论】:
-
你能用小提琴或codepen演示这个问题吗?
-
我添加了一些关于结构的更多细节。这应该足以理解问题。
标签: vuejs2