Vue3使用富文本框tinymce

首先先安装
项目里输入命令

npm install @jsdawn/vue3-tinymce

然后再components文件夹下新建一个TinymceEditor文件夹,新建index.vue,代码如下

<template>
    <div class="tinymce-box">
        <vue3-tinymce v-model="state.content" :setting="state.setting" script-src="/static/tinymce/tinymce.min.js" />
    </div>
</template>

<script setup>
import { defineProps,reactive ,watch,defineEmits} from 'vue';
// 引入组件
import Vue3Tinymce from '@jsdawn/vue3-tinymce';
const props=defineProps({
    modelValue:{
      type:String
    }
})
const emits=defineEmits(['update:modelValue','contentChange'])
const state = reactive({
    content:props.modelValue,
    setting: {
        height: 300,
        toolbar:
            "undo redo | fullscreen | blocks alignleft aligncenter alignright alignjustify | link unlink | numlist bullist | image media table | fontsize forecolor backcolor | bold italic underline strikethrough | indent outdent | superscript subscript | removeformat |",
        toolbar_mode: "sliding",
        quickbars_selection_toolbar:
            "removeformat | bold italic underline strikethrough | fontsize forecolor backcolor",
        plugins: "link image media table lists fullscreen quickbars",
        font_size_formats: "12px 14px 16px 18px",
        link_default_target: "_blank",
        link_title: false,
        nonbreaking_force_tab: true,
        // 以中文简体为例
        language: "zh-Hans",
        language_url:
            "/static/tinymce/langs/zh-Hans.js"
    }
});
watch(()=>state.content,()=>{
   emits('update:modelValue',state.content)
   emits('contentChange',state.content)
},{
    immediate:true,
    deep:true
})

watch(()=>props.modelValue,()=>{
    state.content=props.modelValue;
},{
    immediate:true,
    deep:true
})



</script>
<style scoped lang="scss">
.tinymce-box{

    :deep(.tox-promotion) {
    display: none;
    }
    :deep(.tox-statusbar__branding){
        display: none;
    }
}



</style>

请添加图片描述
在static下新建tinymce文件夹,放置静态文件
最后在main.js中全局注册下组件,各个vue页面可以不再重复引入组件

..
import TinymceEditor from "@/components/TinymceEditor/index.vue";
...
app.component("TinymceEditor",TinymceEditor)
...

最后就可以直接使用了。例子:

<el-form-item label="信息内容 " prop="content">
                   
         <TinymceEditor v-model="form.content" v-if="open" style="width: 100%"></TinymceEditor>
                   
</el-form-item>

效果
请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值