【发布时间】:2017-08-19 12:10:42
【问题描述】:
我在一个 vue 组件中添加了 jquery redactor 插件。该插件工作正常,但我需要访问 html 以便在 Vue 中看到它。
我已经尝试了所有我能想到的方法、计算属性,但我找不到方法。这特别棘手,因为 Redactor 将新的 html 添加到 dom 中,我需要从添加的 html 中获取数据。
目前我收到此错误,this.$emit is not a function。我需要将.redactor-editor 的 html 值放入 prop 中,以便在 vue 数据中可用。 var textContent 在控制台中正确打印,但我无法在 prop 中显示。非常感谢任何帮助。
组件
<template>
<div>
<textarea 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: ['redactorValue'],
mounted: function(){
$('#question-create-form .question-create-editor').redactor({
imageUpload:'/urlGoesHereBro/',
plugins: ['video', 'imagemanager', 'counter', 'limiter'],
buttonsHide:['html', 'formatting', 'deleted', 'indent', 'outdent', 'alignment', 'horizontalrule']
});
},
computed: {
redactorValue: function(){
$('#question-create-form .redactor-editor').on('keyup', function(){
var textContent = $('#question-create-form .redactor-editor').html();
console.log( 'textContent = ' + textContent );
this.$emit('redactorValue', textContent);
});
}
}
};
HTML
<vueredactor></vueredactor>
【问题讨论】:
-
不要将 prop 和计算的名称命名为相同的名称。我希望这是您的
this.$emit不是功能问题的原因。 -
为什么不直接指定数据值并使用它而不是道具?这似乎是一个内部值,而不是一个属性。
-
谢谢我改了,但没有效果
-
德普。我错过了回调。您需要正确调用您的函数。我会添加一个答案。
-
我尝试使用道具,而您在另一个问题上所做的 internalValue 方法在这里不起作用,因为编辑器将新 HTML 添加到 dom 中,所以我不能在模板中使用事件,因为html 不存在于模板中。这就是为什么我试图通过计算属性获取 HTML 并将其发送到道具。
标签: jquery vue.js vuejs2 redactor