nuxt.js中引入tinymce并封装成组件使用(亲测有用)

1 篇文章 0 订阅

nuxt版本如下:

"nuxt": "^2.15.7"

注:本文引入的富文本编辑没有引入图片上传插件,如需引入则参考其他博文进行引入。

1.引入tinymce依赖

本人尝试引用过最新版本的tinymce依赖,但不知道为啥引入的时候会报依赖错误,故经过尝试,引入如下版本的依赖:

npm install @tinymce/tinymce-vue@2.1.0 tinymce@5.3.2

2.在static路径下创建tinymce文件夹,如下:

在这里插入图片描述

3.在node_modules中找到下载好的tinymce文件夹,并将其中的shins文件以及里面的东西复制到static/tinymce中,如下:

node_modules文件夹

在这里插入图片描述

node_modules/tinymce/shins文件夹

在这里插入图片描述

复制到static/tinymce

在这里插入图片描述

4.汉化tinymce,下载tinymce的中文语言包,并放在static/tinymce/langs中,如下:

中文语言包下载地址:

https://www.tiny.cloud/get-tiny/language-packages/

下载完成之后得到:

在这里插入图片描述

解压缩之后得到:

在这里插入图片描述

把langs复制到static/tinymce文件夹下,如下:

在这里插入图片描述

5.在项目工程中的components目录下创建文件tinymce-editor,如下:

在这里插入图片描述

6.在创建的tinymce-editor文件中创建index.vue文件,并添加如下内容:

在这里插入图片描述

index.vue
<template>

  <client-only>

    <editor id="tinymce" v-model="myValue" :init="init" :disabled="disabled"></editor>
  </client-only>

</template>

<script>

import editor from '@tinymce/tinymce-vue'

// 引入tinymce,并在客户端初始化
let tinymce;
if (process.client) {

  tinymce = require('tinymce/tinymce');

  // 样式图标
  require('tinymce/icons/default/icons');

  // 主题样式
  require('tinymce/themes/silver/theme');

  //  插入表格插件
  require('tinymce/plugins/table');

  //  列表插件
  require('tinymce/plugins/lists');

  //  字数统计插件
  require('tinymce/plugins/wordcount');

  // 自动链接插件
  require('tinymce/plugins/autolink');

  // 水平分割线
  require('tinymce/plugins/hr');

  // 预览
  require('tinymce/plugins/preview');
}


export default {

  components: {editor},

  props: {

    //传入一个value,使组件支持v-model绑定
    value: {
      type: String,
      default: ''
    },

    disabled: {
      type: Boolean,
      default: false
    },

    plugins: {
      type: [String, Array],
      default: 'lists  table  wordcount  link  hr  autolink  preview'
    },

    toolbar: {
      type: [String, Array],
      default: 'undo  redo  |  formatselect  |  bold  italic  hr  |  alignleft  aligncenter  alignright  alignjustify  |  bullist  numlist  outdent  indent  table  |  removeformat  preview'
    }

  },

  data() {
    return {
      //初始化配置
      init: {
        // 控件语言
        language_url: '/tinymce/langs/zh_CN.js',
        language: 'zh_CN',
        // 富文本样式
        skin_url: '/tinymce/skins/ui/oxide',
        content_css: '/tinymce/skins/content/default/content.css',
        // 高度
        height: 300,
        plugins: this.plugins,
        // 工具栏,如果是false表示隐藏
        toolbar: this.toolbar,
        // 隐藏最上方menu菜单
        menubar: false,
        // 拼写检查
        browser_spellcheck: true,
        // 去水印
        branding: false,
        statusbar: false, // 隐藏编辑器底部的状态栏
        elementpath: false, // 禁用下角的当前标签路径
        paste_data_images: false,  //  允许粘贴图像
        //此处为图片上传处理函数,这个直接用了base64的图片形式上传图片,
        //如需ajax上传可参考https://www.tiny.cloud/docs/configure/file-image-upload/#images_upload_handler
        images_upload_handler: (blobInfo, success, failure) => {
          const img = 'data:image/jpeg;base64,' + blobInfo.base64()
          success(img)
        }
      },
      myValue: this.value
    }
  },

  created() {

  },

  methods: {

    //添加相关的事件,可用的事件参照文档=>  https://github.com/tinymce/tinymce-vue  =>  All  available  events
    //需要什么事件可以自己增加
    onClick(e) {
      this.$emit('onClick', e, tinymce)
    },

    //可以添加一些自己的自定义事件,如清空内容
    clear() {
      this.myValue = ''
    }

  },

  mounted() { // 用process.client判断一下环境再执行

    this.$nextTick(() => {
      if (process.client) {
        window.tinymce.init({});
      }
    })

  },

  watch: {
    value(newValue) {
      this.myValue = newValue
    },
    myValue(newValue) {
      this.$emit('input', newValue)
    }
  }

}

</script>

<style scoped>

</style>

7.在要使用该富文本的.vue文件中引入组件,如下:

<template>
	  <!-- 大小可自定义 -->
      <div style="width: 100%;height: 500px;">
        <tinymceEditor
          v-model="msg"
          :disabled="disabled"
          ref="editor"
        ></tinymceEditor>
      </div>

</template>
<script>
import tinymceEditor from '~/components/tinymce-editor'

export default {
  components: {
    tinymceEditor
  },
  data (){
    return {
      msg:'富文本编辑器',
      disabled:false
      }
  }
}
</script>
8.效果图如下:

在这里插入图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要在nuxt.js使用three.js,可以按照以下步骤操作: 1. 安装three.js 可以通过npm安装three.js: ``` npm install three ``` 2. 创建一个Vue组件nuxt.js,所有页面都被组织Vue组件。要使用three.js,需要在一个组件引入它。 可以创建一个名为`MyThreeComponent.vue`的组件,并在其引入three.js: ```html <template> <div ref="container"></div> </template> <script> import * as THREE from 'three'; export default { mounted() { this.init(); }, methods: { init() { const container = this.$refs.container; // 创建场景 const scene = new THREE.Scene(); // 创建相机 const camera = new THREE.PerspectiveCamera( 45, container.clientWidth / container.clientHeight, 1, 1000 ); camera.position.set(0, 0, 10); // 创建渲染器 const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(container.clientWidth, container.clientHeight); container.appendChild(renderer.domElement); // 创建一个立方体 const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube); // 渲染场景 renderer.render(scene, camera); }, }, }; </script> ``` 在这个组件,我们创建了一个场景、一个相机、一个渲染器和一个立方体,并将它们渲染到页面上。 3. 在页面使用组件 现在,我们可以在任何一个页面使用这个组件了。例如,在`pages/index.vue`,可以这样使用: ```html <template> <div> <MyThreeComponent /> </div> </template> <script> import MyThreeComponent from '~/components/MyThreeComponent.vue'; export default { components: { MyThreeComponent, }, }; </script> ``` 在这个页面,我们引入了`MyThreeComponent`组件,并将它渲染到页面上。 现在,当我们访问这个页面时,就会看到一个绿色的立方体在屏幕心。 这就是在nuxt.js使用three.js的基本步骤。当然,你可以根据自己的需求进行更多的修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值