VUE集成tinymce-editor 踩坑 Uncaught SyntaxError: Unexpected token

VUE集成tinymce-editor 踩坑

安装tinymce

npm install --save @tinymce/tinymce-vue
npm install --save tinymce

装好后,components里新增tinymce-editor.vue

<template>
  <div class="tinymce-editor">
    <editor v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick">
    </editor>
  </div>
</template>
<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'
import 'tinymce/icons/default/icons' //解决了icons.js 报错Unexpected token '<'
// 编辑器插件plugins
// 更多插件参考:https://www.tiny.cloud/docs/plugins/
import 'tinymce/plugins/image'// 插入上传图片插件
import 'tinymce/plugins/media'// 插入视频插件
import 'tinymce/plugins/table'// 插入表格插件
import 'tinymce/plugins/lists'// 列表插件
import 'tinymce/plugins/wordcount'// 字数统计插件
export default {
  components: {
    Editor
  },
  props: {
    value: {
      type: String,
      default: ''
    },
    // 基本路径,默认为空根目录,如果你的项目发布后的地址为目录形式,
    // 即abc.com/tinymce,baseUrl需要配置成tinymce,不然发布后资源会找不到
    baseUrl: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default: 'lists image media table wordcount'
    },
    toolbar: {
      type: [String, Array],
      default: 'undo redo |  formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat'
    }
  },
  data () {
    return {
      init: {
        language_url: `${this.baseUrl}/tinymce/langs/zh_CN.js`,
        language: 'zh_CN',
        skin_url: `${this.baseUrl}/tinymce/skins/ui/oxide`,
        content_css: `${this.baseUrl}/tinymce/skins/content/default/content.css`,
        // skin_url: `${this.baseUrl}/tinymce/skins/ui/oxide-dark`, // 暗色系
        // content_css: `${this.baseUrl}/tinymce/skins/content/dark/content.css`, // 暗色系
        height: 300,
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        menubar: 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
    }
  },
  mounted () {
    tinymce.init({})
  },
  methods: {
    // 添加相关的事件,可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
    // 需要什么事件可以自己增加
    onClick (e) {
      this.$emit('onClick', e, tinymce)
    },
    // 可以添加一些自己的自定义事件,如清空内容
    clear () {
      this.myValue = ''
    }
  },
  watch: {
    value (newValue) {
      this.myValue = newValue
    },
    myValue (newValue) {
      this.$emit('input', newValue)
    }
  }
}

</script>


引用:随便找个vue页面:


<template>
  <div>
    {{ msg }}
    <tinymce-editor ref="editor"
      v-model="msg"
      :disabled="disabled"
      :base-url="baseUrl"
      :language="language"
      :skin="skin"
      @onClick="onClick">
    </tinymce-editor>
    <button @click="clear">清空内容</button>
    <button @click="disabled = true">禁用</button>
    <button @click="disabled = false">启用</button>
  </div>
</template>

<script>
import TinymceEditor from './tinymce-editor' //改成自己的文件夹
export default {
  components: {
    TinymceEditor
  },
  data () {
    return {
      msg: 'Welcome to Use Tinymce Editor-liubing.me',
      disabled: false,
      baseUrl: process.env.NODE_ENV === 'production' ? '/testtinymce' : '',
      language: 'zh_CN',
      skin: 'oxide'
    }
  },
  methods: {
    // 鼠标单击的事件
    onClick (e, editor) {
      console.log('Element clicked')
      console.log(e)
      console.log(editor)
    },
    // 清空内容
    clear () {
      this.$refs.editor.clear()
    }
  }
}
</script>

这里解决了icons.js 报错Uncaught SyntaxError: Unexpected token <:

import 'tinymce/icons/default/icons'

如果还有其他地方也报同样错,可以直接加上import引用

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
根据提供的引用内容,当浏览器报错"Uncaught SyntaxError: Unexpected token import"时,可能是由于浏览器不支持ES6的模块导入语法导致的。解决这个问题的方法有两种: 1. 使用Babel进行转译:Babel是一个广泛使用的JavaScript编译器,可以将ES6的代码转换为浏览器可以理解的ES5代码。你可以在项目中配置Babel,以便将ES6的模块导入语法转换为ES5的模块导入语法。具体步骤如下: - 安装Babel相关的依赖: ```shell npm install --save-dev @babel/core @babel/preset-env ``` - 在项目根目录下创建一个`.babelrc`文件,并配置Babel的预设: ```json { "presets": ["@babel/preset-env"] } ``` - 在项目中使用Babel进行转译: ```shell npx babel src --out-dir dist ``` - 在转译后的代码中,ES6的模块导入语法会被转换为ES5的模块导入语法,从而解决"Uncaught SyntaxError: Unexpected token import"的问题。 2. 使用Webpack进行打包:Webpack是一个常用的模块打包工具,可以将多个JavaScript文件打包成一个文件,并且可以对代码进行转译和优化。你可以在Webpack的配置中使用Babel进行转译,以解决浏览器不支持ES6的模块导入语法的问题。具体步骤如下: - 安装Webpack和Babel相关的依赖: ```shell npm install --save-dev webpack webpack-cli babel-loader @babel/core @babel/preset-env ``` - 在项目根目录下创建一个`webpack.config.js`文件,并配置Webpack和Babel的相关配置: ```javascript const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'], }, }, }, ], }, }; ``` - 在项目中使用Webpack进行打包: ```shell npx webpack ``` - 打包后的代码会被转译为ES5的模块导入语法,从而解决"Uncaught SyntaxError: Unexpected token import"的问题。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值