vue js将数据传递给组件

时间:2016-05-26 18:39:05

标签: javascript twitter-bootstrap vue.js

您好我将数据传递给组件"模态" ,我使用带有bootstrap-vue组件的模态,我需要传递索引,这是我试图做的事情

<table class="table table-striped" v-show="Questions.length!=0">
<tr v-for="Question in Questions">
  <td>{{$index + 1}}</td>
  <td>{{ Question.text }}</td>
  <td>
  <button v-on:click="showmodal = true"  class="btn btn-default" >Manage</button>
  <modal :show.sync="showmodal"  width="400">
    <div slot="modal-header" class="modal-header">
      <h4 class="modal-title">Manage Question</h4>
    </div>
    <div slot="modal-body" class="modal-body">
    <div class="input-group col-lg-6">
      <input type="text" class="form-control"  value="{{ Questions[$index].text }}"></input>
      <span class="input-group-btn"><button v-on:click="AddQuestion"  class="btn btn-default">Save Question</button></span>
    </div>
    </div>
    <div slot="modal-footer" class="modal-footer">
    <button type="button" class="btn btn-default" v-on:click='showmodal = false'>Exit</button>
    <button type="button" class="btn btn-success" v-on:click='showmodal = false'>Custom Save</button>
    </div>
  </modal>
  <button class="btn btn-danger" v-on:click="DeleteQuestion($index)">Delete</button>
  </td>
</tr>

提示:传递给索引中模态的数据是最后一个索引,即使它在循环中的模态

1 个答案:

答案 0 :(得分:4)

使用Vue道具:http://vuejs.org/guide/components.html#Props

在你的例子中:

<modal :show.sync="showmodal" :index="$index" width="400">
    ...
</modal>

然后在你的Vue模态组件中:

props: ['index']

然后,您就可以像访问其他任何属性一样访问组件内的indexthis.index

相关问题