在Vue.js vue-kanban插件中启用动态输入

时间:2019-05-26 03:32:41

标签: javascript vue.js kanban

我正在尝试启用对Vue.js vue-kanban插件(https://github.com/BrockReece/vue-kanban)的动态输入。到目前为止,我已经成功创建了一种方法,该方法将用户输入推送到动态创建的块中,状态设置为“ on-hold”,以便默认情况下在初始的“ on-hold”阶段渲染所创建的块,然后启用用于拖放到每个阶段。问题是,除了标题之外,我还不得不手动输入每个块的ID。我希望每个块都具有唯一的ID,而不必手动输入一个ID。同样,控制台返回错误,指示未在实例上定义“ title”的属性或方法,但在渲染期间引用了该属性或方法。解决这两个问题的最佳方法是什么?这是我的代码。谢谢!

//App.vue

<template>
  <div id="app">
    <div class="input-container">
      <input type="number" placeholder="id" v-model="id"/>
      <input type="text" placeholder="title" v-model="title"/>
      <button type="button" v-on:click="addBlock()">Add</button>
    </div>
    <kanban-board :stages="stages" :blocks="blocks">
      <div v-for="stage in stages" :slot="stage" :key="stage">
        <h2>{{ stage }}</h2>
      </div>
      <div v-for="block in blocks" :slot="block.id" :key="block.id">
        <div>
          <strong>id:</strong> {{ block.id }}
        </div>
        <div>
          {{ block.title }}
        </div>
      </div>
    </kanban-board>
  </div>
</template>

<script>
export default {
  name: 'app',
  id: null,
  title: null,
  data () {
    return {
      stages: ['on-hold', 'in-progress', 'needs-review', 'approved'],
      blocks: [],
    }
  },
  methods: {
    addBlock(){
      this.blocks.push({
        id: this.id,
        status: "on-hold",
        title: this.title
      });
      this.status = "";
      this.title = "";
    }
  }
}
</script>

<style lang="scss">
  @import './assets/kanban.scss';

  .input-container {
    width: 50%;
    margin: 0 auto;
  }
</style>

1 个答案:

答案 0 :(得分:0)

  

我希望每个块都具有唯一的ID,而不必   手动输入一个。

也许这可以解决问题。

<template>
  <div id="app">
    <div class="input-container">
      <input type="text" placeholder="title" v-model="title"/>
      <button type="button" v-on:click="addBlock()">Add</button>
    </div>
    <kanban-board :stages="stages" :blocks="blocks">
      <div v-for="stage in stages" :slot="stage" :key="stage">
        <h2>{{ stage }}</h2>
      </div>
      <div v-for="block in blocks" :slot="block.id" :key="block.id">
        <div>
          <strong>id:</strong> {{ block.id }}
        </div>
        <div>
          {{ block.title }}
        </div>
      </div>
    </kanban-board>
  </div>
</template>

<script>
export default {
  name: 'app',
  id: 0,
  title: null,
  data () {
    return {
      stages: ['on-hold', 'in-progress', 'needs-review', 'approved'],
      blocks: [],
    }
  },
  methods: {
    addBlock(){
      this.blocks.push({
        id: this.id,
        status: "on-hold",
        title: this.title
      });
      this.id += 1;
      this.status = "";
      this.title = "";
    }
  }
}
</script>