当前位置:首页 > VUE

vue 实现blog

2026-03-06 23:44:45VUE

Vue 实现博客系统

项目初始化

使用 Vue CLI 或 Vite 创建项目:

npm init vue@latest vue-blog
cd vue-blog
npm install

安装必要依赖(如路由、状态管理):

npm install vue-router pinia

路由配置

src/router/index.js 中配置基本路由:

import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import PostList from '../views/PostList.vue'
import PostDetail from '../views/PostDetail.vue'

const routes = [
  { path: '/', component: Home },
  { path: '/posts', component: PostList },
  { path: '/posts/:id', component: PostDetail }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router

状态管理

使用 Pinia 管理博客文章数据:

// stores/postStore.js
import { defineStore } from 'pinia'

export const usePostStore = defineStore('posts', {
  state: () => ({
    posts: [
      { id: 1, title: 'Vue入门', content: '...' },
      { id: 2, title: 'Pinia使用', content: '...' }
    ]
  }),
  actions: {
    addPost(post) {
      this.posts.push(post)
    }
  }
})

文章列表组件

创建 PostList.vue 组件:

<template>
  <div>
    <h1>文章列表</h1>
    <ul>
      <li v-for="post in postStore.posts" :key="post.id">
        <router-link :to="`/posts/${post.id}`">{{ post.title }}</router-link>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { usePostStore } from '../stores/postStore'
const postStore = usePostStore()
</script>

文章详情组件

创建 PostDetail.vue 组件:

<template>
  <div v-if="post">
    <h1>{{ post.title }}</h1>
    <p>{{ post.content }}</p>
  </div>
  <div v-else>
    文章不存在
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { usePostStore } from '../stores/postStore'

const route = useRoute()
const postStore = usePostStore()

const post = computed(() => 
  postStore.posts.find(p => p.id === Number(route.params.id))
)
</script>

添加文章功能

创建文章编辑组件:

vue 实现blog

<template>
  <form @submit.prevent="handleSubmit">
    <input v-model="title" placeholder="标题" required>
    <textarea v-model="content" placeholder="内容" required></textarea>
    <button type="submit">发布</button>
  </form>
</template>

<script setup>
import { ref } from 'vue'
import { usePostStore } from '../stores/postStore'

const title = ref('')
const content = ref('')
const postStore = usePostStore()

const handleSubmit = () => {
  postStore.addPost({
    id: Date.now(),
    title: title.value,
    content: content.value
  })
  title.value = ''
  content.value = ''
}
</script>

样式优化

添加基础样式(建议使用 Tailwind CSS 或 SCSS):

npm install tailwindcss postcss autoprefixer

配置 tailwind.config.js

module.exports = {
  content: ['./index.html', './src//*.{vue,js,ts,jsx,tsx}'],
  theme: {
    extend: {},
  },
  plugins: [],
}

部署准备

配置 vite.config.js 用于生产构建:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  base: '/vue-blog/'
})

构建命令:

vue 实现blog

npm run build

可选扩展功能

  1. 添加 Markdown 支持:

    npm install marked
  2. 实现评论功能:

    // 在postStore中添加comments状态和addComment动作
  3. 用户认证:

    npm install firebase
  4. 数据持久化:

    npm install axios

以上实现提供了基础博客系统的核心功能,可根据需求进一步扩展和完善。

标签: vueblog
分享给朋友:

相关文章

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <templat…

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template>…

vue实现筛选

vue实现筛选

实现筛选功能的基本思路 在Vue中实现筛选功能通常需要结合数据绑定、计算属性和方法。筛选的核心逻辑是根据用户输入的条件过滤原始数据列表,并动态更新显示结果。 数据准备 定义一个数组存储原始数据,另一…