当前位置:首页 > VUE

vue实现文章管理

2026-01-18 03:08:16VUE

实现文章管理的基本功能

使用Vue实现文章管理功能需要结合前端框架和后端API。以下是关键步骤和代码示例:

创建Vue组件管理文章列表

<template>
  <div>
    <h3>文章列表</h3>
    <table>
      <tr v-for="article in articles" :key="article.id">
        <td>{{ article.title }}</td>
        <td>
          <button @click="editArticle(article.id)">编辑</button>
          <button @click="deleteArticle(article.id)">删除</button>
        </td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      articles: []
    }
  },
  mounted() {
    this.fetchArticles()
  },
  methods: {
    fetchArticles() {
      axios.get('/api/articles')
        .then(response => {
          this.articles = response.data
        })
    },
    deleteArticle(id) {
      axios.delete(`/api/articles/${id}`)
        .then(() => {
          this.fetchArticles()
        })
    }
  }
}
</script>

创建文章编辑表单组件

<template>
  <div>
    <h3>{{ editing ? '编辑文章' : '新建文章' }}</h3>
    <form @submit.prevent="submitForm">
      <input v-model="form.title" placeholder="标题">
      <textarea v-model="form.content" placeholder="内容"></textarea>
      <button type="submit">保存</button>
    </form>
  </div>
</template>

<script>
export default {
  props: {
    article: Object,
    editing: Boolean
  },
  data() {
    return {
      form: {
        title: '',
        content: ''
      }
    }
  },
  watch: {
    article: {
      immediate: true,
      handler(val) {
        if (val) {
          this.form = { ...val }
        }
      }
    }
  },
  methods: {
    submitForm() {
      const method = this.editing ? 'put' : 'post'
      const url = this.editing ? `/api/articles/${this.form.id}` : '/api/articles'

      axios[method](url, this.form)
        .then(() => {
          this.$emit('saved')
          if (!this.editing) {
            this.form = { title: '', content: '' }
          }
        })
    }
  }
}
</script>

集成路由管理

在Vue Router中配置文章管理相关路由:

import Vue from 'vue'
import Router from 'vue-router'
import ArticleList from './components/ArticleList'
import ArticleForm from './components/ArticleForm'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/articles',
      component: ArticleList
    },
    {
      path: '/articles/new',
      component: ArticleForm
    },
    {
      path: '/articles/:id/edit',
      component: ArticleForm,
      props: route => ({ editing: true, article: { id: route.params.id } })
    }
  ]
})

实现状态管理

对于复杂应用,可以使用Vuex管理文章状态:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    articles: []
  },
  mutations: {
    SET_ARTICLES(state, articles) {
      state.articles = articles
    },
    ADD_ARTICLE(state, article) {
      state.articles.push(article)
    },
    UPDATE_ARTICLE(state, updatedArticle) {
      const index = state.articles.findIndex(a => a.id === updatedArticle.id)
      if (index !== -1) {
        state.articles.splice(index, 1, updatedArticle)
      }
    },
    DELETE_ARTICLE(state, id) {
      state.articles = state.articles.filter(a => a.id !== id)
    }
  },
  actions: {
    async fetchArticles({ commit }) {
      const response = await axios.get('/api/articles')
      commit('SET_ARTICLES', response.data)
    },
    async createArticle({ commit }, article) {
      const response = await axios.post('/api/articles', article)
      commit('ADD_ARTICLE', response.data)
    },
    async updateArticle({ commit }, article) {
      const response = await axios.put(`/api/articles/${article.id}`, article)
      commit('UPDATE_ARTICLE', response.data)
    },
    async deleteArticle({ commit }, id) {
      await axios.delete(`/api/articles/${id}`)
      commit('DELETE_ARTICLE', id)
    }
  }
})

实现富文本编辑器集成

集成第三方富文本编辑器如Quill:

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

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

export default {
  props: {
    content: String
  },
  mounted() {
    this.quill = new Quill(this.$refs.editor, {
      theme: 'snow',
      modules: {
        toolbar: [
          ['bold', 'italic'],
          ['link', 'blockquote'],
          [{ list: 'ordered' }, { list: 'bullet' }]
        ]
      }
    })

    if (this.content) {
      this.quill.root.innerHTML = this.content
    }

    this.quill.on('text-change', () => {
      this.$emit('update:content', this.quill.root.innerHTML)
    })
  }
}
</script>

vue实现文章管理

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

相关文章

vue实现添加用户

vue实现添加用户

Vue 实现添加用户功能 数据绑定与表单设计 在 Vue 中实现添加用户功能,首先需要设计一个表单,用于收集用户输入的数据。通过 v-model 实现双向数据绑定,确保表单数据与 Vue 实例中的数据…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <tem…

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框…

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Refle…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件,…