如何在vuejs中获取data- *属性的值

时间:2017-02-16 09:04:20

标签: javascript jquery vue.js vuejs2

我有一个按钮,当点击它时会打开一个模态,而在模态中显示的内容则基于传递给按钮的数据属性。

我的按钮,

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

在我的模态中,我有一些按钮,当点击时,我应该使用参数调用vuejs函数,该参数是数据属性。

我的模态按钮,

<div class="modal-footer">
    <button type="button" class="btn btn-danger"
        @click.prevent="deleteItem()" data-dismiss="modal">
        Delete
    </button>
    <button type="button" class="btn btn-warning" data-dismiss="modal">
        <span class='glyphicon glyphicon-remove'></span> Close
    </button>
</div>

在这里,我必须将参数传递给deleteItem(),并且该参数是我从上面的按钮获得的data-id

模态代码

    <div id="deleteModal" 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">Delete</h4>
                </div>
                <div class="modal-body">
                    <div class="deleteContent">
                        Are you Sure you want to delete ?
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn actionBtn btn-danger"
                            @click.prevent="deleteItem()"
                            data-dismiss="modal">
                            Delete <span id="footer_action_button"
                                class='glyphicon glyphicon-trash'> </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>

4 个答案:

答案 0 :(得分:4)

我建议在组件函数中执行console.log(this),然后在单击按钮时调用该函数,以便检查组件的所有属性。

(参见下面的附图,例如上述console.log声明的输出。)

这向您展示了您可以访问持有DOM元素的$el属性。这开辟了很多可能性,例如能够将以下代码添加到组件的mounted生命周期钩子中:

mounted() {
  console.log(this);
  this.myAttribute = this.$el.getAttribute('data-attribute-name');
},

对于此示例,this.myAttribute将在(例如)您的数据属性中定义:

// This value gets attributed during an earlier lifecycle hook.
// It will be overridden in the component's mounted() lifecycle hook.
data() {
  return {
    myAttribute: 'defaultvalue'
  }
}

Vue.js (v2) Lifecycle hooks documentation

在组件内执行console.log(this)时的示例输出:

enter image description here

答案 1 :(得分:0)

一个简单的选项是将id绑定到删除按钮

  <button type="button" class="btn btn-danger"
        @click.prevent="deleteItem(this)"  :data-id=item.id data-dismiss="modal">
        Delete
    </button>

答案 2 :(得分:0)

还请注意,您也可以通过以下方式传递ID:

<button type="button" @click="deleteItem(item.id)">Delete</button>

答案 3 :(得分:0)

HTML

<button type="button" @click.prevent="deleteItem()" :data-id="1" data-dismiss="modal">
Delete <span id="footer_action_button" class='glyphicon glyphicon-trash'> </span>
</button>

Vue

methods:{
  deleteItem: function(){
    var id = event.target.getAttribute('data-id');
    console.log(id) // 1
  }
}

这是演示https://codepen.io/maab16/pen/jONZpVG