首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何点击组件并传递动态数据?

如何点击组件并传递动态数据?
EN

Stack Overflow用户
提问于 2019-05-30 07:41:01
回答 1查看 0关注 0票数 0

我有一个循环组件,它看起来如此:

代码语言:javascript
运行
复制
<light-box @click="passProductId(category.id)" :category-id="categoryId"></light-box>

我的灯箱组件中的代码是:

代码语言:javascript
运行
复制
<template>
    <div>
        <button type="button" class="btn btn-primary btn-sm btn-block" data-toggle="modal" data-target="#newproductmodal">Add new card</button>

        <div class="modal fade" id="newproductmodal" tabindex="-1" role="dialog" aria-labelledby="newproductmodal" aria-hidden="true">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    modal form
                </div>
            </div>
        </div>

    </div>
</template>

在父组件中,方法是:

代码语言:javascript
运行
复制
passProductId(columnId) {
  this.categoryId = columnId;
}

我试过以下:

.native会在模态中触发双重操作,只要我点击某处,该值就会被设置为最后一个值。

组件灯箱处于循环中,我希望每次都传递动态类别ID。

为什么我不能在这里做@click=""?将此值传递给组件会更好的建议是什么?

EN

Stack Overflow用户

发布于 2019-05-30 17:16:59

建议孩子与父母之间的沟通方式是自定义事件,您可以这样做:

代码语言:javascript
运行
复制
//parent component
<light-box @modalClicked="passProductId" :category-id="categoryId"></light-box>
//the method can stay the same
//child
<template>
    <div>
        <button type="button" @click="$emit('modalClicked',categoryId)" class="btn btn-primary btn-sm btn-block" data-toggle="modal" data-target="#newproductmodal">Add new card</button>

        <div class="modal fade" id="newproductmodal" tabindex="-1" role="dialog" aria-labelledby="newproductmodal" aria-hidden="true">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    modal form
                </div>
            </div>
        </div>

    </div>
</template>
//it's important that you receive the "categoryId" as props in the child and thatis the id that your function will get (implicitly that's why it's not inline).
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档