当前位置:首页 > 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实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例:…

vue实现白板

vue实现白板

Vue实现白板功能 使用Vue实现白板功能可以通过HTML5的Canvas API结合Vue的响应式特性来完成。以下是实现白板功能的关键步骤: 安装依赖 在Vue项目中安装必要的依赖,如vue-dr…

vue实现循环

vue实现循环

Vue 实现循环的方法 在 Vue 中,可以通过 v-for 指令实现循环渲染列表或对象。以下是几种常见的用法: 遍历数组 使用 v-for 遍历数组时,可以同时获取当前项和索引: <ul&…

vue实现单击

vue实现单击

Vue 实现单击事件的方法 在 Vue 中实现单击事件可以通过 v-on 指令或简写的 @ 符号绑定事件处理器。以下是几种常见的实现方式: 基础用法 通过 v-on:click 或 @click 绑…

vue实现treetable

vue实现treetable

Vue实现TreeTable的方法 使用第三方组件库(如Element UI) Element UI的el-table组件支持树形表格展示,通过设置row-key和tree-props属性即可实现。…