当前位置:首页 > VUE

vue实现秀米

2026-01-18 02:13:27VUE

Vue 实现类似秀米的富文本编辑器

使用 Vue 实现类似秀米的富文本编辑器需要结合富文本编辑库和自定义组件。以下是实现方案:

方案一:基于 Quill 的扩展 安装 Quill 和相关插件:

npm install quill vue-quill-editor

创建自定义编辑器组件:

<template>
  <div class="editor-container">
    <quill-editor
      v-model="content"
      :options="editorOptions"
      @change="onEditorChange"
    />
    <div class="toolbar">
      <button @click="insertTemplate('header')">插入标题</button>
      <button @click="insertTemplate('image')">插入图片</button>
    </div>
  </div>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'

export default {
  components: { quillEditor },
  data() {
    return {
      content: '',
      editorOptions: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline'],
            ['image', 'video']
          ]
        }
      }
    }
  },
  methods: {
    onEditorChange({ html }) {
      console.log(html)
    },
    insertTemplate(type) {
      if(type === 'header') {
        this.content += '<h1>标题文字</h1>'
      }
    }
  }
}
</script>

方案二:使用 Tiptap 编辑器 Tiptap 是基于 ProseMirror 的现代编辑器:

vue实现秀米

npm install @tiptap/vue-2 @tiptap/starter-kit

实现组件:

<template>
  <editor-content :editor="editor" />
</template>

<script>
import { Editor, EditorContent } from '@tiptap/vue-2'
import StarterKit from '@tiptap/starter-kit'

export default {
  components: { EditorContent },
  data() {
    return {
      editor: null
    }
  },
  mounted() {
    this.editor = new Editor({
      content: '<p>初始内容</p>',
      extensions: [
        StarterKit
      ],
      onUpdate: ({ editor }) => {
        console.log(editor.getHTML())
      }
    })
  }
}
</script>

实现秀米特色功能

模板系统 创建可复用的模板组件:

vue实现秀米

<template>
  <div class="template-gallery">
    <div 
      v-for="(temp, index) in templates"
      :key="index"
      @click="applyTemplate(temp)"
    >
      <img :src="temp.preview">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      templates: [
        { id: 1, preview: 'header.png', html: '<div class="xiumi-header">...</div>' }
      ]
    }
  },
  methods: {
    applyTemplate(template) {
      this.$emit('insert', template.html)
    }
  }
}
</script>

样式隔离 使用 Scoped CSS 和 BEM 命名规范:

/* 编辑器样式 */
.editor-container >>> .ql-container {
  font-family: 'PingFang SC';
}

/* 模板样式 */
.xiumi-template {
  margin: 10px 0;
  border: 1px dashed #eee;
}

与后端数据交互

实现内容保存和加载:

// API 方法
export default {
  methods: {
    async saveContent() {
      await axios.post('/api/content', { html: this.content })
    },
    async loadContent(id) {
      const res = await axios.get(`/api/content/${id}`)
      this.content = res.data.html
    }
  }
}

移动端适配

添加响应式处理:

// 检测设备类型
const isMobile = window.innerWidth < 768

// 动态调整工具栏
editorOptions: {
  toolbar: isMobile ? 
    [['bold', 'italic']] : 
    fullToolbarOptions
}

以上方案提供了在 Vue 中实现类似秀米编辑器的基本框架,可根据实际需求扩展更多功能如素材库、多列布局等。核心在于结合现有富文本编辑器进行二次开发,通过组件化方式实现模板系统。

标签: vue
分享给朋友:

相关文章

vue slot实现

vue slot实现

vue slot 的实现方法 在 Vue 中,slot 是一种内容分发机制,允许父组件向子组件传递模板内容。以下是几种常见的 slot 实现方式: 默认 slot 默认 slot 是最基础的 slo…

vue实现cs

vue实现cs

Vue 实现 CS(Client-Side)渲染的常见方法 Vue.js 是一个用于构建用户界面的渐进式框架,默认采用客户端渲染(CSR)。以下是几种常见的实现方式: 使用 Vue CLI 创建项目…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…

vue 实现拖拉

vue 实现拖拉

Vue 实现拖拽功能的方法 使用 HTML5 原生拖放 API HTML5 提供了原生的拖放 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 dr…

vue 菜单实现

vue 菜单实现

Vue 菜单实现方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for动态生成菜单 通过数据驱动的方式动态渲染菜单项,适合菜单内容可能变化的场景: <t…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…