当前位置:首页 > VUE

vue实现博客

2026-03-07 04:55:27VUE

Vue 实现博客的基本步骤

项目初始化

使用 Vue CLI 或 Vite 创建项目:

npm create vue@latest blog-project
cd blog-project
npm install

安装必要依赖

安装路由和状态管理库:

npm install vue-router pinia

项目结构设计

典型的博客项目结构:

src/
├── assets/
├── components/
│   ├── BlogPost.vue
│   ├── NavBar.vue
│   └── CommentSection.vue
├── views/
│   ├── HomeView.vue
│   ├── PostListView.vue
│   └── PostDetailView.vue
├── store/
│   └── blog.js
├── router/
│   └── index.js
└── App.vue

路由配置

// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import PostListView from '../views/PostListView.vue'
import PostDetailView from '../views/PostDetailView.vue'

const routes = [
  { path: '/', component: HomeView },
  { path: '/posts', component: PostListView },
  { path: '/posts/:id', component: PostDetailView }
]

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

export default router

状态管理

// store/blog.js
import { defineStore } from 'pinia'

export const useBlogStore = defineStore('blog', {
  state: () => ({
    posts: [],
    currentPost: null
  }),
  actions: {
    async fetchPosts() {
      // API调用获取文章列表
    },
    async fetchPost(id) {
      // API调用获取单篇文章
    }
  }
})

博客文章组件

<!-- components/BlogPost.vue -->
<template>
  <div class="post">
    <h2>{{ post.title }}</h2>
    <p>{{ post.content }}</p>
    <div class="meta">
      <span>发布于 {{ post.date }}</span>
    </div>
  </div>
</template>

<script setup>
defineProps({
  post: {
    type: Object,
    required: true
  }
})
</script>

实现Markdown支持

安装markdown解析器:

npm install marked

创建Markdown渲染组件:

<!-- components/MarkdownRenderer.vue -->
<template>
  <div v-html="compiledMarkdown"></div>
</template>

<script setup>
import { marked } from 'marked'
import { computed } from 'vue'

const props = defineProps({
  content: {
    type: String,
    required: true
  }
})

const compiledMarkdown = computed(() => {
  return marked(props.content)
})
</script>

实现评论功能

<!-- components/CommentSection.vue -->
<template>
  <div class="comments">
    <h3>评论</h3>
    <div v-for="comment in comments" :key="comment.id">
      <p>{{ comment.author }}: {{ comment.content }}</p>
    </div>
    <form @submit.prevent="addComment">
      <input v-model="newComment" placeholder="添加评论">
      <button type="submit">提交</button>
    </form>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const comments = ref([])
const newComment = ref('')

const addComment = () => {
  if (newComment.value.trim()) {
    comments.value.push({
      id: Date.now(),
      author: '匿名用户',
      content: newComment.value
    })
    newComment.value = ''
  }
}
</script>

与后端API集成

使用axios进行API调用:

npm install axios

示例API调用:

import axios from 'axios'

const fetchPosts = async () => {
  try {
    const response = await axios.get('/api/posts')
    return response.data
  } catch (error) {
    console.error('获取文章列表失败:', error)
    return []
  }
}

部署准备

创建生产版本:

vue实现博客

npm run build

部署到静态网站托管服务,如Vercel、Netlify或GitHub Pages。

标签: 博客vue
分享给朋友:

相关文章

vue实现点击样式

vue实现点击样式

Vue 实现点击样式的方法 在 Vue 中实现点击样式可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-bind:class 动态绑定类名 通过数据驱动的方式动态切换类名,结合 CS…

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓、面…

vue实现主页

vue实现主页

Vue 实现主页的基本步骤 创建一个 Vue 主页通常涉及以下几个关键步骤,从项目初始化到页面布局和功能实现。 初始化 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI…

vue分类实现

vue分类实现

Vue分类实现方法 在Vue中实现分类功能通常涉及数据分组、动态渲染和交互逻辑。以下是几种常见的实现方式: 基于计算属性的分类 通过计算属性对原始数据进行分组处理,适合静态或少量数据分类: com…

vue实现动态

vue实现动态

Vue 实现动态路由的方法 在 Vue 中实现动态路由通常涉及以下几个关键步骤: 使用 Vue Router 的动态路由匹配 通过 Vue Router 的 :param 语法可以实现动态路由匹配。…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…