百度富文本編輯器vue

百度富文本編輯器文檔:http://fex.baidu.com/ueditor/#start-config
下載ueditor官網:http://ueditor.baidu.com/website/download.html

下面我們先在html中引用百度編輯器
1.在html中引入

    <!-- 配置文件 -->
   <script type="text/javascript" src="ueditor.config.js"></script>

   <!-- 編輯器源碼文件 -->
   <script type="text/javascript" src="ueditor.all.js"></script>
    <!-- 語言文件 -->
   <script type="text/javascript" charset="utf-8" src="lang/zh-cn/zh-cn.js"></script>

2.在body中加富文本編輯器的容器,并初始化

<!-- 加載編輯器的容器 -->
    <script id="container" name="content" type="text/plain">
        我的初始化
    </script>
  <!-- 在script中初始化 -->
<!-- 實例化編輯器 -->
    <script type="text/javascript">
        var ue = UE.getEditor('container',{
            serverUrl:"http://test.io/php/controller.php?action=config"http://這里是上傳圖片后端處理文件地址(自行替換),如果不使用圖片上傳,則不需要配置
        });
    </script>

3.此時如果后端配置好了,就已經可以使用了,后端需要修改上傳限制以及上傳返回路徑
舉例子:

{
    "imageUrlPrefix": "http://test.io", /* 圖片訪問路徑前綴 ,加入以后獲取富文本編輯器內容時,圖片地址會以這個前綴開頭*/
    "imagePathFormat": "/image/{yyyy}{mm}{dd}/{time}{rand:6}",/* 這是上傳后的路徑*/

4.最后放到服務器的樣式:(其中圖片是我自己在編輯器頁面加入的)


微信圖片_20190306162116.png

vue中使用ueditor

1.先下載富文本編輯器,并將所需要的文件放到指定文件夾中,我 是放在plugins中.


image.png

這里需要注意,在開發(fā)的時候如果開啟了webpack-dev-server,在開發(fā)的時候可能是顯示的,但是打包以后到生產環(huán)境的時候會找不到dialog等文件,需要你在webpack.config.prod.js文件中修改,加入CopyWebpackPlugin插件,將plugins中文件復制到對應目錄dist/js目錄下:

 new webpack.DefinePlugin({
      'process.env': {
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    new CopyWebpackPlugin([
      {
        from: path.join(__dirname, './src/plugins/ueditor'),
        to: path.join(__dirname,"./dist/js/"),
        ignore: ['.*']
      }
    ]),

2.在入口文件中導入我們需要的文件,我這里是main.js

// 導入編輯器
import './plugins/ueditor/ueditor.config.js'
import './plugins/ueditor/ueditor.all.js'
import './plugins/ueditor/lang/zh-cn/zh-cn.js'
import './plugins/ueditor/ueditor.parse.js'

3.為了多次使用富文本編輯器,我們使用vue的組件

<template>
  <div class="editor-box">
    <script id="editor" type="text/plain"></script>
  </div>
</template>
<script>
  export default {
    name: 'UE',
    data () {
      return {
        editor: null
      }
    },
    props: {
      defaultMsg: {
        type: String
      },
      config: {
        type: Object
      }
    },
    mounted() {
      const _this = this;
      this.editor = UE.getEditor('editor', this.config); // 初始化UE
      this.editor.addListener("ready", function () {
        _this.editor.setContent(_this.defaultMsg); // 確保UE加載完成后,放入內容。
      });
    },
    methods: {
      getUEContent() { // 獲取內容方法
        return this.editor.getContent()
      }
    },
    destroyed() {
      this.editor.destroy();
    }
  }
</script>
<style>
.editor-box{
    padding: 0 40px;
}

</style>

4.在所需的頁面中導入組件

<template>
    <div>
              <div class="editor-container">
                  <UE :defaultMsg='defaultMsg' :config='config' ref="ue"></UE>
              </div>
     </div>
<template>
<script>
import UE from '../../../components/subcom/ueditor.vue';
  export default {
  components: {
        UE
    },
    data () {
      return {
        defaultMsg:'測試',
            config:{
                serverUrl:"http://test.io/php/controller.php?action=config",
                autoHeightEnabled: true,
                autoFloatEnabled: true
            },
      }
    },
  
    mounted() {
     this.$refs.ue.style="width:auto";
    },
    methods: {

    },

  }
</script>
<style>

</style>

這是我自己的項目中顯示的富文本編輯器


image.png

5.這里補充一下,編輯器寬度自適應的問題,解決方案就是在config參數里面修改:initialFrameWidth:'100%',即可解決自適應問題。
6解決自動將div轉換成p標簽,在config參數里面修改:allowDivTransToP:false,即可解決。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容