【问题标题】:How to get data attributes in vue.js如何在 vue.js 中获取数据属性
【发布时间】:2017-07-09 09:20:28
【问题描述】:

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

我的按钮,

<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'));
        });

【问题讨论】:

    标签: javascript jquery twitter-bootstrap vuejs2 vue.js


    【解决方案1】:

    好吧,我不知道这是否是一种解决方法,但您可以做的是创建一个 modal 组件,然后通过 props 或更好地使用 slots 将数据传递给它,就像在模态示例 I已链接。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 2020-11-26
      • 2020-10-07
      • 2019-06-13
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多