繁体   English   中英

如何在 vue.js 中获取数据属性

[英]How to get data attributes in vue.js

我有一个按钮,单击它时会弹出一个模式。 模式中的数据与按钮的数据属性一起传递。

我的按钮,

<button class="edit-modal btn btn-info" data-toggle="modal"
                                data-target="#editModal" :data-id=item.id :data-name=item.name>
                                <span class="glyphicon glyphicon-edit"></span> Edit
                            </button>

它有一些数据属性,如idname

我的模态,

<div id="editModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Edit</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="control-label col-sm-2">ID:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="fid" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-2">Name:</label>
                            <div class="col-sm-10">
                                <input type="name" class="form-control" id="n">
                            </div>
                        </div>
                    </form>
                    <div class="modal-footer">
                        <button type="button" class="btn actionBtn btn-success"
                            @click.prevent="updateItem()" data-dismiss="modal">
                            Update <span id="footer_action_button"
                                class='glyphicon glyphicon-check'> </span>
                        </button>
                        <button type="button" class="btn btn-warning"
                            data-dismiss="modal">
                            <span class='glyphicon glyphicon-remove'></span> Close
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

实际上,我正在使用 javascript,并且模式正在显示我想要的 id 和 name。 而且我还需要将这些数据属性传递给update()函数。

如何在vuejs中使用数据属性解决问题。

$(document).on('click', '.edit-modal', function() {
            $('#fid').val($(this).data('id'));
            $('#n').val($(this).data('name'));
        });

好吧,我不知道这是否是一种解决方法,但是您可以做的是创建一个模态组件,然后通过道具将数据传递给它,或者更好的是使用我链接的模态示例中的插槽

这应该为您指明正确的方向。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM