【发布时间】:2017-08-16 19:34:52
【问题描述】:
我正在尝试将 redactor 用作 vuejs 2 组件,但我遇到了很多麻烦,因为一旦加载 redactor 就会创建一个新 div,所以我无法在该 div 上收听 keyup 事件。请看我下面的代码。
Vue 组件
<template>
<div>
<textarea @keyup="internalValue" class="form-control question-create-editor" id="question_description" placeholder="Go wild with all the details here - make image upload work" rows="3">
</div>
</template>
<script>
export default {
props: ['value'],
data () {
return {
internalValue: this.value
}
},
mounted: function(){
$(function() {
// $( document ).ready(function() {
$('#question-create-form .question-create-editor').redactor({
imageUpload:'/urlGoesHereBro/',
plugins: ['video', 'imagemanager', 'counter', 'limiter'],
buttonsHide:['html', 'formatting', 'deleted', 'indent', 'outdent', 'alignment', 'horizontalrule']
});
$('#question-create-form .redactor-editor').attr('v-model', 'questionDescription');
// });
});
},
watch: {
internalValue(v){
this.$emit($('.redactor-editor').html(), v);
}
}
};
</script>
注册组件
Vue.component('vueredactor', require('./components/redactor-qa.vue'));
初始化 VUE
var App = new Vue({
el: '#app',
data: {
questionDescription: null
}
});
HTML
<div id="app">
<vueredactor v-model="questionDescription"></vueredactor>
</div>
【问题讨论】:
-
正确包装一个小部件需要做很多事情。这对你来说将是一次学习冒险。 :) 不要使用文档级别的 jQuery 选择器,而是使用
this.$el作为您的顶级 DOM 元素。参考这里:vuejs.org/v2/api/#mounted -
感谢您的信息,我将开始我的新冒险:)
-
对于遇到相同问题的任何人,请参阅此问题中的答案:stackoverflow.com/questions/43035340/…
标签: vue.js vuejs2 vue-component redactor