vue2引入富文本tinymce

根据业务需求,需要引入富文本,选择tinymce
文末可直接拉取代码,开箱即用

安装

官方文档:tinymce官网
需要在官方后台申请key,才可以使用全功能
本次示例是基于vue2项目的介绍

如果是vue3,使用 
npm install --save tinymce "@tinymce/tinymce-vue@^5"

如果是vue2,使用 
npm install --save tinymce "@tinymce/tinymce-vue@^3"

使用

最基本的使用
PS:如果报错,检查一下版本,可能版本不对应

<template>
  <Editor
     api-key="your-api-key"
    :init="{
      plugins: 'lists link image table code help wordcount'
    }"
  />
</template>

<script>
import Editor from '@tinymce/tinymce-vue';
export default {
  components: { Editor },v
}
</script>

如果需要绑定值可获取可传递,可以加个v-model=“value”
在这里插入图片描述

完善功能init设置

具体啥功能啥介绍自己翻文档看tinymce官网

完整代码

在这里插入图片描述

<template>
  <Editor
    api-key="your-key"
    v-model="editValue"
    :init="setting"
  />
</template>

<script>
import Editor from '@tinymce/tinymce-vue';
export default {
  components: { Editor },
  props: {
    height: { type: String, default: '400px' },
    value: { type: String, default: '' },
  },
  data() {
    return {
      editValue: '',
    };
  },
  computed: {
    setting() {
      return {
        plugins:
          'preview importcss searchreplace autolink autosave save directionality code visualblocks visualchars fullscreen image link media codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount help charmap quickbars emoticons template',
        editimage_cors_hosts: ['picsum.photos'],
        menubar: 'file edit view insert format tools table help', // 菜单
        toolbar:
          'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | align numlist bullist | link image | table media | lineheight outdent indent| forecolor backcolor removeformat | charmap emoticons | code fullscreen preview | save print | pagebreak anchor codesample | ltr rtl', // 导航栏
        link_list: [
          // 配置链接列表
          { title: 'My page 1', value: 'https://www.tiny.cloud' },
          { title: 'My page 2', value: 'http://www.moxiecode.com' },
        ],
        autosave_ask_before_unload: true,
        autosave_interval: '30s',
        autosave_prefix: '{path}{query}-{id}-',
        autosave_restore_when_empty: false,
        autosave_retention: '2m',
        toolbar_mode: 'sliding',
        // quickbars_selection_toolbar: 'bold italic | quicklink h2 h3 blockquote quickimage quicktable', // 配置快速工具栏
        noneditable_class: 'mceNonEditable',
        contextmenu: 'link image table',
        image_advtab: true,
        images_upload_url: '/home/file/upload', // 图片上传的 URL
        images_upload_handler: function (blobInfo, success, failure) {
          this.handleImageUpload(blobInfo, success, failure);
        }.bind(this), // 处理图片上传的方法
        height: this.height,
      };
    },
  },
  // watch: {
  //   editValue(v) {
  //     console.log(v);
  //   },
  // },
  methods: {
    // 上传图片
    handleImageUpload(blobInfo, success, failure) {
      console.log('2', blobInfo, success, failure);
      const xhr = new XMLHttpRequest();
      xhr.withCredentials = false;
      xhr.open('POST', '/home/file/upload'); // 后端处理图片上传的 URL
      xhr.onload = () => {
        if (xhr.status === 0) {
          success(xhr.responseText); // 图片上传成功
        } else {
          this.$message.error('上传失败');
          failure && failure('上传失败'); // 图片上传失败
        }
      };
      xhr.send(blobInfo.blob());
    },
  },
};
</script>

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 2.x 与 tinymce 文本编辑器的集成相对简单。以下是一些步骤可以帮助你在 Vue.js 2.x 中使用 tinymce: 1. 安装 tinymce:你可以通过 npm 或 yarn 安装 tinymce,运行以下命令: ```bash npm install tinymce # 或 yarn add tinymce ``` 2. 在组件中引入 tinymce 并初始化: ```vue <template> <div> <textarea v-model="content" :id="editorId"></textarea> </div> </template> <script> import tinymce from 'tinymce/tinymce'; export default { data() { return { content: '', editorId: 'my-editor', // 指定一个唯一的 id }; }, mounted() { tinymce.init({ selector: `#${this.editorId}`, plugins: 'advlist autolink lists link image charmap print preview hr anchor', toolbar: 'undo redo | formatselect | bold italic backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | image', height: 500, setup: (editor) => { editor.on('change', () => { this.content = editor.getContent(); }); }, }); }, beforeDestroy() { tinymce.get(this.editorId).destroy(); }, }; </script> ``` 在上述示例中,我们在 mounted 钩子中初始化了 tinymce 编辑器,并且使用 v-model 指令将编辑器的内容绑定到 Vue 实例上的 content 属性。在 beforeDestroy 钩子中,我们销毁了 tinymce 编辑器。 你可以根据需要调整 tinymce 的配置项以满足你的需求。可以在 tinymce 的官方文档中了解更多配置选项和插件的使用方法:[https://www.tiny.cloud/docs/](https://www.tiny.cloud/docs/) 希望这能帮到你!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值