当前位置:首页 > VUE

vue实现新闻编辑页

2026-01-21 00:19:08VUE

Vue 新闻编辑页实现步骤

基础项目搭建

使用 Vue CLI 或 Vite 创建项目,安装必要依赖:

npm install vue-router axios quill

路由配置

在 router/index.js 中配置编辑页路由:

{
  path: '/news/edit/:id?',
  name: 'NewsEdit',
  component: () => import('../views/NewsEdit.vue')
}

富文本编辑器集成

使用 Quill 实现富文本编辑功能:

<template>
  <div class="editor-container">
    <div ref="editor" style="height: 400px"></div>
  </div>
</template>

<script>
import Quill from 'quill'
import 'quill/dist/quill.snow.css'

export default {
  mounted() {
    this.quill = new Quill(this.$refs.editor, {
      theme: 'snow',
      modules: {
        toolbar: [
          ['bold', 'italic', 'underline'],
          [{ 'header': [1, 2, 3] }],
          ['link', 'image']
        ]
      }
    })
  }
}
</script>

表单数据绑定

实现新闻数据的双向绑定:

vue实现新闻编辑页

<template>
  <form @submit.prevent="handleSubmit">
    <input v-model="news.title" placeholder="标题">
    <input v-model="news.author" placeholder="作者">
    <select v-model="news.category">
      <option v-for="cat in categories" :value="cat">{{ cat }}</option>
    </select>
  </form>
</template>

<script>
export default {
  data() {
    return {
      news: {
        title: '',
        content: '',
        author: '',
        category: ''
      },
      categories: ['政治', '经济', '科技', '体育']
    }
  }
}
</script>

图片上传处理

实现图片上传功能:

methods: {
  uploadImage() {
    const input = document.createElement('input')
    input.type = 'file'
    input.accept = 'image/*'
    input.onchange = async () => {
      const formData = new FormData()
      formData.append('image', input.files[0])

      try {
        const res = await axios.post('/api/upload', formData)
        const range = this.quill.getSelection()
        this.quill.insertEmbed(range.index, 'image', res.data.url)
      } catch (error) {
        console.error(error)
      }
    }
    input.click()
  }
}

数据提交处理

实现表单提交逻辑:

methods: {
  async handleSubmit() {
    this.news.content = this.quill.root.innerHTML

    try {
      if (this.$route.params.id) {
        await axios.put(`/api/news/${this.$route.params.id}`, this.news)
      } else {
        await axios.post('/api/news', this.news)
      }
      this.$router.push('/news/list')
    } catch (error) {
      console.error('保存失败', error)
    }
  }
}

初始化数据加载

编辑时加载现有数据:

vue实现新闻编辑页

async created() {
  if (this.$route.params.id) {
    const res = await axios.get(`/api/news/${this.$route.params.id}`)
    this.news = res.data
    this.quill.clipboard.dangerouslyPasteHTML(this.news.content)
  }
}

样式优化

添加基本样式:

.editor-container {
  border: 1px solid #ddd;
  border-radius: 4px;
}

.ql-toolbar {
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  background-color: #f5f5f5;
}

.ql-container {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

表单验证

添加基础验证逻辑:

methods: {
  validateForm() {
    if (!this.news.title.trim()) {
      this.$message.error('请输入标题')
      return false
    }
    if (!this.news.content.trim()) {
      this.$message.error('请输入内容')
      return false
    }
    return true
  },

  async handleSubmit() {
    if (!this.validateForm()) return
    // 提交逻辑...
  }
}

响应式布局

添加响应式设计:

@media (max-width: 768px) {
  .editor-container {
    margin: 0 10px;
  }

  .ql-toolbar .ql-formats {
    margin-right: 5px;
  }
}

标签: 编辑新闻
分享给朋友:

相关文章

基于vue实现新闻前台

基于vue实现新闻前台

实现Vue新闻前台的关键步骤 环境准备与项目初始化 使用Vue CLI创建项目,安装必要依赖如vue-router、axios、element-ui等。配置基础路由和页面结构。 新闻数据获取 通过a…

vue实现双击编辑

vue实现双击编辑

Vue 实现双击编辑功能 基本实现思路 通过监听元素的 dblclick 事件触发编辑状态,结合 v-if 和 v-else 切换显示模式。以下是核心实现方案: <template&g…

vue实现表格编辑

vue实现表格编辑

Vue 实现表格编辑的方法 在 Vue 中实现表格编辑功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定数据 通过 v-model 绑定表格数据,结合 v-for 动态渲染…

vue实现界面编辑

vue实现界面编辑

Vue 实现界面编辑的方法 使用双向数据绑定 Vue 的核心特性是双向数据绑定,通过 v-model 指令可以轻松实现表单元素的编辑功能。例如,对于输入框的编辑: <template>…

vue如何实现编辑

vue如何实现编辑

实现编辑功能的步骤 在Vue中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是常见的实现方式: 数据绑定与表单处理 使用v-model实现双向数据绑定,将编辑内容与Vue实例中的数据属性关联…

vue实现新闻播报

vue实现新闻播报

Vue实现新闻播报功能 新闻播报功能可以通过Vue结合动态数据渲染和动画效果实现。以下是几种常见方法: 数据动态渲染 使用v-for指令循环渲染新闻列表,结合定时器实现自动轮播: <…