当前位置:首页 > VUE

vue实现文章管理

2026-01-18 03:08:16VUE

实现文章管理的基本功能

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

创建Vue组件管理文章列表

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中配置文章管理相关路由:

vue实现文章管理

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 中实现遮罩层可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-show 或 v-if 控制显示 通过 Vue 的指令动态控制遮罩层的显示与隐藏。…

vue slot实现

vue slot实现

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

vue实现全景

vue实现全景

Vue 实现全景效果的方法 使用 Vue 实现全景效果通常需要结合第三方库或框架,以下是几种常见的方法: 使用 Three.js 和 Vue-Three.js Three.js 是一个强大的 3D…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…