Vue组件中的jQuery插件:无法将值传递给prop
我在vue组件中添加了jquery redactor插件。 该插件工作正常但我需要访问html所以我可以在Vue中看到它。
我已经尝试了我能想到的一切,方法,计算属性,但我找不到办法。 它特别棘手,因为Redactor在dom中添加了新的html,我需要从添加的html中获取数据。
目前我收到这个错误, this.$emit is not a function
。 我需要将.redactor-editor
的html值放入prop中,以便在vue数据中可用。 var textContent
在控制台中正确打印出来但我无法在prop
显示。 任何帮助是极大的赞赏。
零件
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
this.$emit is not a function
问题,因为this
是指向window
。
我keyup
定义移到了mounted
。
export default { data(){ return { redactorValue: null } }, mounted: 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').on('keyup', function(){ this.redactorValue = $('#question-create-form .redactor-editor').html(); }.bind(this)); } };
只是为了添加@BertEvans精湛的答案。 这是一个将道具和v模型添加回组合的解决方案。 这允许您将redactor内容作为值获取,并可以访问根目录中的数据。
零件
JS
Vue.component('vueredactor', require('./components/redactor.vue')); var App = new Vue({ el: '#app', data: { redactorContent: null } });
HTML
这是我使用Imperavi的Redactor Editor 3工作的VueJS 2组件。 在同一视图中支持多编辑器,但编辑器需要唯一的名称/ ID。 不需要jQuery库。
RedactorEditor.vue
App.vue
import Vue from 'vue' import RedactorEditor from './components/RedactorEditor.vue' // app const app = new Vue({ el: '#my-app', data: { editorButtons: ['undo', 'redo', 'bold', 'italic', 'lists', 'link'], editorPlugins: [], beyondgrammarSettings: {} editorHTMLContent: '', }, components: { RedactorEditor } }
HTML用法
使用vee-validate的自定义Validator示例: https : //gist.github.com/matinfo/52214f7f34ce6b746b483f0f92e6b5e5