Vue 安装 wangEditor 富文本编辑器

最近遇到Vue项目需要添加一个富文本编辑,纠结了好久,最后选择 wangEditor ,然后整理了一些wangEditor  的使用教程

1.安装

npm install @wangeditor/editor --save

#vue2 
npm install @wangeditor/editor-for-vue --save

2.使用

<template>
    <div style="border: 1px solid #ccc;">
        <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
        />
        <Editor
            style="height: 500px; overflow-y: hidden;"
            v-model="html"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
        />
    </div>
</template>

<!-- 一定要记得引入style -->
<style src="@wangeditor/editor/dist/css/style.css"></style>

<script>
  import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

  export default Vue.extend({
    components: { Editor, Toolbar },
    data() {
      return {
        editor: null,
        html: '<p>hello</p>',
        toolbarConfig: {
            toolbarKeys : ['bold', 'underline', 'headerSelect', 'italic', 'through', 'code', 'sub', 'sup', 'clearStyle', 'color', 'bgColor', 'fontSize', 'fontFamily', 'indent', 'delIndent', 'justifyLeft', 'justifyRight', 'justifyCenter', 'justifyJustify', 'lineHeight',  'divider', 'insertLink', 'codeBlock', 'blockquote','redo', 'undo', 'fullScreen',  'bulletedList', 'numberedList', 'insertTable'
        ]
      },
        editorConfig: { placeholder: '请输入内容...' },
        mode: 'default', // or 'simple'
      }
    },
    methods: {
      onCreated(editor) {
        this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错

        //打印所有工具
        //console.log(this.editor.getAllMenuKeys());
      },
    },
    mounted() {
      // 模拟 ajax 请求,异步渲染编辑器
      setTimeout(() => {
        this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
      }, 1500)
    },
    beforeDestroy() {
      const editor = this.editor
      if (editor == null) return
      editor.destroy() // 组件销毁时,及时销毁编辑器
    },
  })
</script>

复制以上代码,即可在页面上使用富文本编辑器

但是功能并非上面这一点点,内含很强大功能,有的功能只有在使用的时候,才会去研究。

文档如下:

官方教程

wangEditor官网

Github地址

### Vue集成WangEditor富文本编辑器 #### 安装配置 为了在Vue项目中集成WangEditor富文本编辑器,首先需要安装`@wangeditor/editor`以及其对应的样式包。可以通过npm来完成这一步骤。 ```bash npm install @wangeditor/editor --save npm install @wangeditor/editor-for-vue --save ``` 接着,在项目的入口文件(通常是main.js),引入必要的CSS资源[^2]: ```javascript import '@wangeditor/editor/dist/css/style.css' ``` #### 使用方法 创建一个新的组件用于初始化编辑器实例,并将其挂载至页面上指定的位置。下面是一个简单的例子说明如何操作: ```html <template> <div id="editor-container"></div> </template> <script> // 导入 Editor 和 createEditor 函数 import { createEditor } from '@wangeditor/editor' export default { name: 'MyEditor', data() { return { editor: null, } }, mounted() { this.editor = createEditor({ selector: '#editor-container', // 编辑区域的选择器 config: {}, // 配置项 mode: 'default' // 模式,默认即可 }) }, beforeDestroy() { if (this.editor == null) return; this.editor.destroy(); // 组件销毁前要记得释放掉编辑器实例 } } </script> ``` 对于更复杂的场景比如自定义上传图片等功能,则可以在config参数里设置相应的回调函数处理逻辑。 #### 示例 这里给出一段完整的代码片段作为参考,展示了怎样在一个标准的Vue单文件组件内使用WangEditor编辑器,并实现了基本的文字输入功能。 ```html <!-- MyRichTextEditor.vue --> <template> <div class="richText"> <!-- 这里放置实际渲染出来的编辑框 --> <div ref="toolbar" style="border-bottom: 1px solid #ccc;"></div> <div ref="editor" style="text-align:left;min-height:300px;border:1px dashed #ccc;margin-top:-1px;padding-left:8px;"></div> </div> </template> <script> import WangEditor from '@wangeditor/editor'; import '@wangeditor/editor/dist/css/style.css'; export default { props: ['value'], watch: { value(newValue, oldValue){ const editorValue = this.editor.txt.html(); if(editorValue !== newValue && !this.isChangeByInner){ this.editor.txt.html(newValue); } } }, methods:{ changeHandler(newHtml){ this.$emit('input', newHtml); } }, mounted(){ var _self=this; // 创建工具栏和编辑区对象 let E = WangEditor; this.editor = new E(this.$refs.editor); // 设置 onchange 回调函数,将数据同步到父级组件 this.editor.config.onchange = function(html){ _self.changeHandler(html); }; // 自定义菜单配置... // 创建编辑器 this.editor.create(); // 初始化内容 setTimeout(() => { this.editor.txt.html(_self.value || ''); }, 500); }, destroyed(){ this.editor.destroy(); } }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值