当前位置:首页 > 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中实现多级组件通常涉及父子组件嵌套,通过props向下传递数据,通过$emit向上传递事件。多级组件的核心是递归或动态组件渲染。 <!-- 父组件 Parent…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现权限

vue实现权限

Vue 实现权限管理的方法 在 Vue 项目中实现权限管理通常涉及路由控制、按钮权限和接口权限。以下是几种常见的实现方式: 路由权限控制 通过动态路由的方式,根据用户权限加载不同的路由配置。可以使用…

vue 实现页面返回

vue 实现页面返回

实现页面返回的方法 在Vue中实现页面返回功能可以通过多种方式实现,以下是几种常见的方案: 使用Vue Router的go方法 通过Vue Router的go方法可以控制浏览器的历史记录导航。rou…

vue 实现登录跳转

vue 实现登录跳转

实现登录跳转的核心逻辑 在Vue中实现登录跳转通常涉及路由守卫、状态管理和API交互。以下是具体实现方式: 配置路由守卫 在路由配置文件中添加beforeEach守卫,检查用户是否已登录。未登录时重…