将summernote 与vuejs 结合使用

Vic*_*hov 5 javascript jquery summernote vue.js rubaxa-sortable

我尝试创建具有以下功能的待办事项列表:

  1. 可分类项目
  2. 每个项目中的 WYSIWYG 编辑器
  3. 待办事项模型中项目编辑器存储中的每个更改

我做了 1 和 2 但不能做 3。我只能更改任务数组中第一个任务的标题

这是我的代码

应用程序.js

Vue.directive('summernote', {
  bind: function() {
    this.editor = $(this.el).summernote({
      airMode: true,
      disableDragAndDrop: true,
      popover: {
        air: [
          ['style', ['bold', 'italic', 'underline', 'clear']]
        ]
      },
      callbacks: {
        onChange: function(contents, $editable) {
          vm.tasks[0].title = contents;
        }
      }
    });
  }
});

var vm = new Vue({
  el: '#todos',

  ready: function (value) {
    Sortable.create(this.$el.firstChild, {
      draggable: 'li',
      animation: 500,
      onUpdate: function(e) {
        var oldPosition = e.item.getAttribute('data-id');
        var newPosition = this.toArray().indexOf(oldPosition);
        vm.tasks.splice(newPosition, 0, vm.tasks.splice(oldPosition, 1)[0]);
      }
    });
  },

  data: {
    tasks: [
      { title: 'First task', done: true },
      { title: 'Second task', done: true },
      { title: 'Third task', done: false }
    ],
    newTask: '',
    editTask: null
  },

  methods: {
    addTask (e) {
      e.preventDefault();
      this.tasks.push({ title: this.newTask, done: false });
      this.newTask = '';
    },

    removeTask (index) {
      this.tasks.splice(index, 1);
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

索引.html

    <div class="container">
    <div id="todos"><ul class="list-group">
        <li
            v-for="task in tasks"
            class="list-group-item"
            data-id="{{$index}}"
            >
          <span
                @click="task.done = !task.done"
                class="glyphicon glyphicon-ok"
                ></span>
                <div v-summernote>{{ task.title }}</div>
          <span @click="removeTask($index)" class="close">&times;</span>
        </li>
    </ul>

    <form @submit="addTask">
      <input v-model="newTask" class="form-control" type="text" placeholder="Add some task">
    </form>
    <div v-summernote></div>
    <pre>{{tasks | json}}</pre>
    <pre>{{editor}}</pre>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

如何编辑和保存每个项目的 Summernote 内容?这是工作示例

Roy*_*y J 3

我认为首选方法是构建一个组件(或使用现有的组件),该组件将具有 props 等。但是,事实证明,this您可以使用指令内部的内部属性:_scope它在终端指令示例中进行了记录(至少提到了)。

你的绑定函数变成:

bind: function() {
  const scope = this._scope;
  this.editor = $(this.el).summernote({
    airMode: true,
    disableDragAndDrop: true,
    popover: {
      air: [
        ['style', ['bold', 'italic', 'underline', 'clear']]
      ]
    },
    callbacks: {
      onChange: function(contents, $editable) {
        scope.task.title = contents;
      }
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

  • 连接 vue 和 Summernote 的库看起来已被废弃,因为它仅适用于 vue-1。它不仅在描述中指定,而且我还查看了源代码,在此我确认它在 Vue 2 上不起作用 (3认同)