当前位置:首页 > 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 的表单验证功能:

vue实现文章管理

<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组件,包含奖品列表、抽奖按钮和结果显示区域。使用随机数生成器选择奖品。 <template> <div> <…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…

vue递归实现

vue递归实现

vue递归实现方法 在Vue中实现递归组件通常用于树形结构、嵌套菜单等场景。以下是具体实现方式: 使用组件name属性递归 定义一个组件时,通过name属性让组件可以在自身模板中调用自己: <…

vue实现组件

vue实现组件

Vue 组件实现基础 Vue 组件是可复用的 Vue 实例,用于封装 UI 和逻辑。通过 .vue 文件或直接注册组件实现。 单文件组件 (SFC) 示例 <template>…

vue实现im通信

vue实现im通信

Vue 实现 IM 通信的方法 选择通信协议 WebSocket 是实现实时 IM 通信的常用协议,相比 HTTP 更适合双向通信。可使用原生 WebSocket 或第三方库如 Socket.IO。…

vue实现文字纠错

vue实现文字纠错

Vue实现文字纠错功能 实现文字纠错功能通常需要结合前端Vue框架和后端自然语言处理(NLP)服务。以下是几种常见的实现方法: 使用第三方API服务 注册并获取API密钥(如百度AI开放平台、腾讯云…