AVue插件:使用ueditor實現網站內容編輯和優化

隨著人們對於網站內容要求的不斷提高,網站的內容編輯和優化也變得越來越重要。為了能夠讓網站擁有更好的內容編輯和優化,我們可以使用ueditor來實現。而使用ueditor來編輯網站內容的好處在於ueditor的靈活性和多樣化,例如可以添加視頻、圖片等多媒體內容。本文將從多個方面對於AVue插件的使用ueditor來實現網站內容編輯和優化進行詳細闡述。

一、開發環境

在使用ueditor插件之前,首先需要將AVue項目下載至本地,並安裝vue-cli。然後,通過npm安裝相應的插件:

<!-- 安裝vue-ueditor-wrap -->
npm i vue-ueditor-wrap -S

<!-- 安裝ueditor -->
npm i ueditor -S
 
<!-- 解決iOS環境問題 -->
npm install babel-plugin-transform-remove-strict-mode --save-dev

二、引入ueditor插件並進行配置

為了使用ueditor插件來編輯網站內容,需要在組件中引入相應的代碼:

<!-- 引入ueditor代碼 -->
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>test ue</title>
    <style>
        *{margin:0;padding:0;}
    </style>
    <!-- 配置文件 -->
    <script type="text/javascript" src="./static/ueditor/ueditor.config.js"></script>
    <!-- 編輯器源碼文件 -->
    <script type="text/javascript" src="./static/ueditor/ueditor.all.js"></script>
    <script type="text/javascript" src="./static/ueditor/lang/zh-cn/zh-cn.js"></script>
</head>
<body>
    <div id="editor" style="width:100%;height:520px;"></div>
    <button id="btn"></button>
</body>
</html>

引入代碼後,需要進行相關的配置,例如ueditor.config.js配置:

window.UEDITOR_HOME_URL = "/static/ueditor/";

module.exports = {
  UEDITOR_HOME_URL: '/static/UEditor/',
  configUrl: '/api/ueditor/getConfig',
  imageUrl: '/api/ueditor/uploadImage',
  imagePath: '',
  videoUrl: '/api/ueditor/uploadVideo'
}

三、如何進行富文本編輯

使用ueditor插件進行富文本編輯非常簡單。只需要在組件中引入相應的代碼即可:

<template>
    <div>
        <vue-ueditor-wrap
            v-model="editorContent"
            :config="editorConfig"
            :init="editorInit">
        </vue-ueditor-wrap>
    </div>
</template>
 
<script>
    import VueUeditorWrap from 'vue-ueditor-wrap';
 
    export default {
        data() {
            return {
                editorContent: '',
                editorConfig: {
                    // todo editor config
                },
                editorInit: {
                    // todo
                }
            }
        },
        components: {
            'vue-ueditor-wrap': VueUeditorWrap
        }
    }
</script>

代碼中的vue-ueditor-wrap組件需要在組件中進行引入,同時需要設置相應的參數:

  • v-model: 綁定富文本編輯器的內容
  • config: 配置ueditor的參數
  • init: 編輯器初始化時的一些配置

四、將富文本內容渲染在頁面上

在使用ueditor插件進行富文本編輯之後,需要將編輯器中的內容在網站中進行渲染。這需要使用v-html進行渲染:

<div v-html="editorContent"></div>

以上代碼會將編輯器中的內容進行渲染,並且在網站中進行顯示。

五、添加多媒體文件

ueditor插件不僅僅可以添加文本,還可以添加多媒體文件,如圖片、視頻等。首先,在ueditor.config.js中進行相關的配置:

// 配置圖片模塊
imageUrl: '/api/ueditor/uploadImage',
imagePath: '',
imageFieldName: 'file',
imageMaxSize: 2048000,
imageAllowFiles: ['.png', '.jpg', '.jpeg', '.gif', '.bmp'],
imageCompressEnable: true,
imageCompressBorder: 1600,
imageInsertAlign: 'none',
imageUrlPrefix: '',

然後,在代碼中加入相應的代碼:

<template>
  <div>
    <el-button size="small" @click="insertImageDialogVisible = true">選擇圖片</el-button>
    <el-dialog
      title="插入圖片"
      :visible.sync="insertImageDialogVisible"
      :append-to-body="true">
      <el-form :model="insertImage" ref="insertImageForm" :rules="insertImageRules">
        <el-form-item label="圖片">
          <el-upload
            class="avatar-uploader"
            name="avatar"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :action="imageAction"
            :headers="headers"
            v-model="insertImage.imageUrl">
            <img v-if="insertImage.imageUrl" :src="insertImage.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-button size="small" @click="insertImage.imageUrl = ''">移除圖片</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="insertImageDialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="insertImageHandler">插入</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      insertImageDialogVisible: false,
      insertImage: {
        imageUrl: ''
      },
      insertImageRules: {
        imageUrl: [
          { required: true, message: '請選擇圖片', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    imageAction() {
      return `${process.env.VUE_APP_BASE_API}/aliyun/oss/policy/image`
    },
    headers() {
      return {
        Authorization: getToken()
      }
    }
  },
  methods: {
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
 
      if (!isJPG) {
        this.$message.error('上傳文件只能是 JPG 或 PNG 格式!');
        return false;
      }
      if (!isLt2M) {
        this.$message.error('上傳文件大小不能超過 2MB!');
        return false;
      }
      return true
    },
    insertImageHandler() {
      this.insertImageDialogVisible = false;
      this.$refs['editor'].insertImg(this.insertImage.imageUrl)
    }
  }
}
</script>

以上代碼中添加了一個選擇圖片的按鈕,選擇完圖片後會彈出一個dialog,將選擇的圖片插入到ueditor插件中。

六、使用ueditor實現網站內容編輯和優化的完整代碼:

<template>
<div>
<el-row>
<el-col :span="18">
<vue-ueditor-wrap
v-model="editorContent"
:config="editorConfig"
:init="editorInit"
ref="editor"
/>
</el-col>
<el-col :span="6">
<el-button size="small" @click="insertImageDialogVisible = true">選擇圖片</el-button>
<el-dialog
title="插入圖片"
:visible.sync="insertImageDialogVisible"
:append-to-body="true">
<el-form :model="insertImage" ref="insertImageForm" :rules="insertImageRules">
<el-form-item label="圖片">
<el-upload
class="avatar-uploader"
name="avatar"
:show-file-list="false"
:before-upload="beforeUpload"
:action="imageAction"
:headers="headers"
v-model="insertImage.imageUrl">
<img v-if="insertImage.imageUrl" :src="insertImage.imageUrl" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
<el-button size="small" @click="insertImage.imageUrl = ''">移除圖片</el-button>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button size="small" @click="insertImageDialogVisible = false">取消</el-button>
<el-button size="small" type="primary" @click="insertImageHandler">插入</el-button>
</span>
</el-dialog>
</el-col>
</el-row>
<el-button type="primary" @click="submit"></el-button>
</div>
</template>

<script>
import VueUeditorWrap from 'vue-ueditor-wrap';

export default {
data() {
return {
editorContent: '',
editorConfig: {
UEDITOR_HOME_URL: '/static/ueditor/',
serverUrl: '/api/ueditor/server',
toolbars: [
[
'fullscreen', 'source', '|', 'undo', 'redo', '|',
'bold', 'italic', 'underline', 'strikethrough', 'blockquote', 'forecolor', 'backcolor', '|',
'fontfamily', 'fontsize', 'paragraph', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
'link', 'unlink', 'anchor', '|',
'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', '|',
'simpleupload', 'insertimage', 'imagecrop', 'horizontal', 'date', 'time', 'spechars', '|',
'map', 'gmap', 'background', 'insertvideo', 'attachment', 'insertframe', '|',
'searchreplace', 'pasteplain', '|',
'preview', 'help', 'drafts'
]
],
autoHeightEnabled: true,
emotionLocalization: true,
elementPathEnabled: false,
enableContextMenu: false,
serverParam: {
csrf_token: '',
port: ''

}
},
editorInit: {
// mediaUrl: '/ueditor/php/controller.php?action=media',
// videoUrl: '/ueditor/php/controller.php?action=video',
// fileUrl: '/ueditor/php/controller.php?action=file'
},
insertImageDialogVisible: false,
insertImage: {
imageUrl: ''
},
insertImageRules: {
imageUrl: [
{ required: true, message: '請選擇圖片', trigger: 'change' }
]

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/303545.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-31 11:50
下一篇 2024-12-31 11:50

相關推薦

  • Python爬蟲可以爬哪些網站

    Python是被廣泛運用於數據處理和分析領域的編程語言之一。它具有易用性、靈活性和成本效益高等特點,因此越來越多的人開始使用它進行網站爬取。本文將從多個方面詳細闡述,Python爬…

    編程 2025-04-29
  • 網站為什麼會被黑客攻擊?

    黑客攻擊是指利用計算機技術手段,入侵或者破壞計算機信息系統的一種行為。網站被黑客攻擊是常見的安全隱患之一,那麼,為什麼網站會被黑客攻擊呢?本文將從不同角度分析這個問題,並且提出相應…

    編程 2025-04-29
  • Python七年級內容用法介紹

    本文將從多個方面對Python七年級內容進行詳細闡述。 一、安裝Python 要使用Python進行編程,首先需要在計算機上安裝Python。Python可以在官網上免費下載。下載…

    編程 2025-04-29
  • 如何用Python訪問網站

    本文將從以下幾個方面介紹如何使用Python訪問網站:網路請求、POST請求、用戶代理、Cookie、代理IP、API請求。 一、網路請求 Python有三種主流的網路請求庫:ur…

    編程 2025-04-29
  • 如何將Python開發的網站變成APP

    要將Python開發的網站變成APP,可以通過Python的Web框架或者APP框架,將網站封裝為APP的形式。常見的方法有: 一、使用Python的Web框架Django Dja…

    編程 2025-04-28
  • Codemaid插件——讓你的代碼優美整潔

    你是否曾為了混雜在代碼里的冗餘空格、重複代碼而感到煩惱?你是否曾因為代碼缺少注釋而陷入困境?為了解決這些問題,今天我要為大家推薦一款Visual Studio擴展插件——Codem…

    編程 2025-04-28
  • 如何在伺服器上運行網站

    想要在伺服器上運行網站,需要按照以下步驟進行配置和部署。 一、選擇伺服器和域名 想要在伺服器上運行網站,首先需要選擇一台雲伺服器或者自己搭建的伺服器。雲伺服器會提供更好的穩定性和可…

    編程 2025-04-28
  • Kong 使用第三方的go插件

    本文將針對Kong使用第三方的go插件進行詳細闡述。首先,我們解答下標題的問題:如何使用第三方的go插件?我們可以通過編寫插件來達到此目的。 一、插件架構介紹 Kong的插件系統采…

    編程 2025-04-28
  • Python獲取Flutter上內容的方法及操作

    本文將從以下幾個方面介紹Python如何獲取Flutter上的內容: 一、獲取Flutter應用數據 使用Flutter提供的Platform Channel API可以很容易地獲…

    編程 2025-04-28
  • Python少兒編程的學習內容

    Python被譽為是最適合新手入門的編程語言之一,它簡潔易懂,同時涵蓋了廣泛的編程知識。Python的少兒編程課程也因其易學性和實用性越來越受到家長和孩子們的歡迎。接下來我們將從多…

    編程 2025-04-28

發表回復

登錄後才能評論