当前位置:首页 > 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 3(Composition API)或 Vue 2(Options API)作为前端框架,搭配 Vue Router 实现路由管理,V…

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue怎么实现文件树

vue怎么实现文件树

Vue 实现文件树的方法 使用递归组件 递归组件是处理嵌套数据结构的理想方式。文件树通常包含文件夹和文件的嵌套结构,适合用递归组件实现。 <template> <ul>…

vue实现录音文件播放

vue实现录音文件播放

使用 Vue 实现录音文件播放 准备工作 确保项目中已安装必要的依赖,如 vue-audio-recorder 或原生 Web Audio API。录音文件播放通常涉及录音、存储和播放三个核心环节。…

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于…