当前位置:首页 > VUE

vue实现文章管理

2026-02-18 19:35:36VUE

Vue 实现文章管理功能

文章管理是内容管理系统的核心功能,通常包括文章的增删改查、分类、标签、富文本编辑等。以下是通过 Vue 实现文章管理的关键步骤和技术方案。

技术栈选择

  • 前端框架: Vue 3(Composition API)
  • UI 组件库: Element Plus(或 Ant Design Vue)
  • 状态管理: Pinia(或 Vuex)
  • HTTP 客户端: Axios
  • 富文本编辑器: WangEditor 或 TinyMCE
  • 路由: Vue Router

核心功能实现

文章列表展示

<template>
  <el-table :data="articles" style="width: 100%">
    <el-table-column prop="title" label="标题" width="180" />
    <el-table-column prop="category" label="分类" width="180" />
    <el-table-column prop="createdAt" label="发布时间" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getArticles, deleteArticle } from '@/api/article'

const articles = ref([])

onMounted(async () => {
  articles.value = await getArticles()
})

const handleEdit = (article) => {
  router.push(`/article/edit/${article.id}`)
}

const handleDelete = async (id) => {
  await deleteArticle(id)
  articles.value = articles.value.filter(item => item.id !== id)
}
</script>

富文本编辑器集成

<template>
  <div>
    <div ref="editor" style="text-align:left"></div>
    <el-button @click="submit">提交</el-button>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import E from 'wangeditor'

const editor = ref(null)
let editorInstance = null

onMounted(() => {
  editorInstance = new E(editor.value)
  editorInstance.create()
})

const submit = () => {
  const content = editorInstance.txt.html()
  // 提交到后端
}
</script>

状态管理设计

使用 Pinia 管理文章状态:

// stores/article.js
import { defineStore } from 'pinia'

export const useArticleStore = defineStore('article', {
  state: () => ({
    articles: [],
    currentArticle: null
  }),
  actions: {
    async fetchArticles() {
      this.articles = await getArticles()
    },
    setCurrentArticle(article) {
      this.currentArticle = article
    }
  }
})

路由配置

// router/index.js
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/articles',
    name: 'ArticleList',
    component: () => import('@/views/ArticleList.vue')
  },
  {
    path: '/article/create',
    name: 'ArticleCreate',
    component: () => import('@/views/ArticleEdit.vue')
  },
  {
    path: '/article/edit/:id',
    name: 'ArticleEdit',
    component: () => import('@/views/ArticleEdit.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

API 封装

// api/article.js
import axios from 'axios'

export const getArticles = () => axios.get('/api/articles')
export const getArticle = (id) => axios.get(`/api/articles/${id}`)
export const createArticle = (data) => axios.post('/api/articles', data)
export const updateArticle = (id, data) => axios.put(`/api/articles/${id}`, data)
export const deleteArticle = (id) => axios.delete(`/api/articles/${id}`)

表单验证

使用 Element Plus 的表单验证功能:

<template>
  <el-form :model="form" :rules="rules" ref="formRef">
    <el-form-item label="标题" prop="title">
      <el-input v-model="form.title"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <div ref="editor"></div>
    </el-form-item>
  </el-form>
</template>

<script setup>
const formRef = ref(null)
const form = reactive({
  title: '',
  content: ''
})

const rules = reactive({
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' },
    { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
  ]
})

const submit = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      await createArticle(form)
    }
  })
}
</script>

注意事项

  1. 安全性: 富文本内容需要做 XSS 过滤
  2. 性能优化: 列表页实现分页和虚拟滚动
  3. SEO: 如需 SEO 考虑,可使用 SSR 方案(如 Nuxt.js)
  4. 文件上传: 实现图片上传功能时,注意文件大小限制和格式验证

以上方案提供了 Vue 实现文章管理系统的核心功能模块,可根据实际需求进行扩展和调整。

vue实现文章管理

标签: 文章vue
分享给朋友:

相关文章

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天…

vue实现自定义登录

vue实现自定义登录

实现自定义登录的基本步骤 在Vue中实现自定义登录功能通常需要结合前端和后端技术。以下是一个基本的实现流程: 创建登录表单组件 使用Vue的单文件组件创建一个登录表单,包含用户名和密码输入框以及提…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue 实现excel

vue 实现excel

Vue 实现 Excel 功能 前端导出 Excel 使用 xlsx 库可以轻松实现前端 Excel 导出功能。安装依赖: npm install xlsx 导出 Excel 示例代码: im…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选…