【发布时间】:2025-11-22 12:00:02
【问题描述】:
我有这个基本设置
<div v-for="n in 4">
<some-component @on-some-event="onSomeEvent(n)"></some-component>
</div>
on-some-event 在some-component 内调度。但我需要知道这些组件中的哪些发送了消息。使用上面的设置,只有n 被传递到方法中。并且事件发送的数据无处可去。
我想对函数进行插值,使方法看起来像这样
onSomeEvent(n){
return (obj)=>{
console.log(`component ${n} sent ${obj}`);
};
}
但是用{{}} 包裹onSomeEvent 会引发警告:attribute interpolation is not allowed in Vue.js directives and special attributes.
我可以将n 索引传递给组件,但这似乎不太优雅,因为我可能无法修改some-component
我对 Vue 有点陌生,所以也许我缺少这类东西的一些核心功能?
【问题讨论】:
标签: vue.js