首页 > 其他分享 > vue3 门户网站搭建6-wangeditor

vue3 门户网站搭建6-wangeditor

时间:2023-03-02 09:11:06浏览次数:47  
标签:solid wangeditor 门户网站 height instance result vue3 config border

门户网站的新闻、公告等文章,内容可配置,故引入 wagneditor

 

1、安装: npm i wangeditor

 

2、方便调用,抽成组件:

<template>
  <div ref='editor'></div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, getCurrentInstance, onUpdated } from 'vue';
import WangEditor from 'wangeditor';
import { getItem, StorageKey } from "@/utils/storage";
import { GetImageUploadUrl } from "@/api/admin";

const { proxy } = getCurrentInstance();
const editor = ref(null);
let params = defineProps([ 'data' ]);

let instance
onMounted(() => {
  createEditor();
});

onUpdated(() => {
  if (instance) {
    instance.txt.html(params.data)
  }
})

const createEditor = () => {
  instance = new WangEditor(editor.value)
  instance.config.showLinkImg = false
  instance.config.showLinkImgAlt = false
  instance.config.showLinkImgHref = false
  instance.config.uploadImgMaxSize = 2 * 1024 * 1024 // 2M
  instance.config.uploadFileName = 'file'
  instance.config.height = 700;
  // 图片上传需添加token到请求头
  instance.config.uploadImgShowBase64 = true;
  instance.config.uploadImgHeaders = {
    token: getItem(StorageKey.token)
  }
  // 图片返回格式不同,需要自定义返回格式
  instance.config.uploadImgHooks = {
    // 图片上传并返回了结果,想要自己把图片插入到编辑器中
    // 例如服务器端返回的不是 { errno: 0, data: [...] } 这种格式,可使用 customInsert
    customInsert: function(insertImgFn, result) {
      // console.log('result', result)
      // result 即服务端返回的接口
      // insertImgFn 可把图片插入到编辑器,传入图片 src ,执行函数即可
      if (result.data && result.data.length) {
        result.data.forEach(item => insertImgFn(item))
      }
    }
  }
  // 图片上传的地址url
  instance.config.uploadImgServer = GetImageUploadUrl();
  Object.assign(instance.config, {
    // 输入内容会触发change事件
    onchange(e) {
      console.log('change', e)
    },
  })
  instance.create()
}

onBeforeUnmount(() => {
  instance.destroy();
  instance = null;
})

</script>

 

界面调用即可看到效果:

 

除了可编辑内容外,当然还要将编辑的内容渲染出来:(部分样式遇到问题,未渲染成功:

 

查看代码发现是对应的 class 不存在,源码中又没有 css 文件可以直接引入,都是些 less 文件。。。只能手动抽一下:

@mixin wangeditor {
  blockquote {
    display: block;
    border-left: 8px solid #d0e5f2;
    padding: 5px 10px;
    margin: 10px 0;
    line-height: 1.4;
    font-size: 100%;
    background-color: #f1f1f1;
  }

  ul, ol {
    padding-left: 20px;
  }

  table {
    border-top: 1px solid #ccc;
    border-left: 1px solid #ccc;

    td, th {
      border-bottom: 1px solid #ccc;
      border-right: 1px solid #ccc;
      padding: 3px 5px;
      min-height: 30px;
      height: 30px;
    }

    th {
      border-bottom: 2px solid #ccc;
      text-align: center;
      background-color: #f1f1f1;
    }
  }

  /*分割线样式*/
  hr {
    cursor: pointer;
    display: block;
    height: 0;
    border: 0;
    border-top: 3px solid #ccc;
    margin: 20px 0;
  }

  pre {
    line-height: 1.5;
    overflow: auto;
    background-color: #f1f1f1;
    code {
      display: block;
    }
  }

  /* 代码段 */
  code {
    display: inline-block;
    background-color: #f1f1f1;
    border-radius: 3px;
    padding: 3px 5px;
    margin: 0 3px;
  }
}

 

再看渲染,样式大体相同了:

标签:solid,wangeditor,门户网站,height,instance,result,vue3,config,border
From: https://www.cnblogs.com/guofan/p/17158842.html

相关文章

  • vue3的ref、reactive、toRefs特性详解
    了解ref()、reactive()这两个特性之前,我们先回顾一下vue2中data和method方法。在vue2中我们定义一个响应式变量name,通过点击事件handle来改变name的值是通过如下方式写的。......
  • Vue3 reactive的理解
    1.什么是reactive?reactive是Vue3中提供实现响应式数据的方法.在Vue2中响应式数据是通过defineProperty来实现的.而在Vue3响应式数据是通过ES6的Proxy来实现的2.rea......
  • vue3+ElementPlus 后台布局搭建
    一、https://element-plus.gitee.io/zh-CN/官网  二、后台布局Layui  代码示例如下<template><divclass="app_container"><divclass="common-layo......
  • 使用unplugin-auto-import自动导入插件优化vite开发vue3应用
    为什么要使用unplugin-auto-import插件? 使用vite编写vue3代码时,使用compositionapi函数、VueRouter、pinia状态管理等官方API需要在页面中显式引入。而使用unplug......
  • vue3 路由
      vue2在2023年12月份开始停止维护,说实话,很苦逼,很多人根本还停留在vue2思想的时代,包括我自己 。之前一直在写vue的服务端渲染,好长时间已经忘了vue-router创......
  • Vue3 状态管理之pinia
    什么是Pinia在vue3之前我们最常用的状态管理就是Vuex,当然在vue3中也有相对应的Vuex的版本。但是还是推荐使用Pinia,原因有以下几点:语法简单,mutations不再存在。无......
  • vue3+openlayes实现离线地图加载
    概述OpenLayers使在任何网页中放置动态地图变得容易。它可以显示从任何来源加载的地图图块、矢量数据和标记。OpenLayers的开发旨在进一步使用各种地理信息。它是完全免......
  • Vue3 + vite + Ts + pinia + 实战
    Pinia起始于2019年11月左右的一次实验,其目的是设计一个拥有组合式API的Vue状态管理库。从那时起,我们就倾向于同时支持Vue2和Vue3,并且不强制要求开发者使用组......
  • vue3中style标签内的一些样式使用
    /*vue3中style标签内的一些样式使用1、使用变量作为css的属性值例如:设置元素的字体大小及字体颜色<scriptsetup>constdata=reactive({fontSize:12,color:"......
  • vue3 门户网站搭建5-图标
    奈何element自带的图标太少,不够用,故打算使用 vite-plugin-svg-icons组件来封装svg-icon。ps:ui框架选用的 element-ui,为了能跟vue3更好的结合,还得装个 elemen......