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/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

发表回复

登录后才能评论