当前位置:首页 > 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 ssr

实现 vue ssr

Vue SSR 实现方法 Vue SSR(Server-Side Rendering)通过服务器端渲染 Vue 应用,提升首屏加载速度和 SEO 友好性。以下是核心实现方法: 基础配置 安装必要依赖…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template&…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

vue实现验证

vue实现验证

Vue 表单验证实现方法 在Vue中实现表单验证可以通过多种方式完成,以下是常见的几种方法: 使用Vuelidate库 Vuelidate是一个轻量级的Vue表单验证库,安装后可以通过简单的配置实现…

lua 实现vue

lua 实现vue

在 Lua 中实现类似 Vue 的响应式框架需要模拟 Vue 的核心功能,包括数据绑定、模板渲染和依赖追踪。以下是实现的关键思路和代码示例: 响应式数据绑定 使用 Lua 的 __index 和 _…