如何在vue.draggable中更新data()

时间:2019-08-24 09:38:30

标签: vue.js vue.draggable

我知道这可能是基本内容...但是使用Vue.Draggable 2.23时我无法更新数据。在下面的代码中,axios调用返回的4个列表与data()现在返回的4个列表完全相同,但是当然还有内容。我一直以某种方式不断获取“属性或方法“ lane0”未在实例上定义,但在渲染期间被引用。”在App.vue中:

<script>
import draggable from "vuedraggable";
import axios from "axios";
export default {
  name: "two-lists",
  display: "Two Lists",
  order: 1,
  components: {
    draggable
  },

  data() {
    return {
        "lane0": [],
        "lane1": [],
        "lane2": [],
        "lane3": []
  }
    },
  mounted () {
    axios
      .get('http://localhost:8000/pylims/get_sequencable_lanes/10/S4')
      .then(response => (this.data= response.data))
  },
  methods: {

    log: function(evt) {
      window.console.log(evt);
    }
  }
}
</script>

根据要求提供HTML部分(App.vue中的模板):

<template>
  <div class="row">
    <div class="col-3">
      <h3>Draggable 1</h3>
      <draggable class="list-group" :list="lane0" group="people" @change="log">
        <div
          class="list-group-item"
          v-for="(element, index) in lane0"
          :key="element.sample_name"
        >
          {{ element.sample_name }} {{ index }}
        </div>
      </draggable>
    </div>

    <div class="col-3">
      <h3>Draggable 2</h3>
      <draggable class="list-group" :list="lane1" group="people" @change="log">
        <div
          class="list-group-item"
          v-for="(element, index) in lane1"
          :key="element.sample_name"
        >
          {{ element.sample_name }} {{ index }}
        </div>
      </draggable>
    </div>

    <div class="col-3">
      <h3>Draggable 3</h3>
      <draggable class="list-group" :list="lane2" group="people" @change="log">
        <div
          class="list-group-item"
          v-for="(element, index) in lane2"
          :key="element.sample_name"
        >
          {{ element.sample_name }} {{ index }}
        </div>
      </draggable>
    </div>
<br><br>
{{ lane0 }}
    <br><br>
{{ lane1 }}
    <br><br>
{{ lane2 }}
    <br><br>
{{ lane3 }}

  </div>
</template>

1 个答案:

答案 0 :(得分:0)

Tobias G.在评论中指出,并在此处进行了清晰的解释:https://vuejs.org/v2/cookbook/using-axios-to-consume-apis.html通过将通道包装在对象中,然后从mount()函数内部对其进行更新来解决此问题,如下所示:< / p>

 data() {
    return {
        lanes: {
          "lane0": [],
          "lane1": [],
          "lane2": [],
          "lane3": []
        }
  }
    },
  mounted () {
    axios
      .get('http://localhost:8000/pylims/get_sequencable_lanes/10/S4')
      .then(response => (this.lanes = response.data))
  },

然后在HTML部分中使用它,如下所示:

<draggable class="list-group" :list="lanes.lane0" group="people" @change="log">