当前位置:首页 > 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-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

jquery实现vue

jquery实现vue

jQuery 和 Vue.js 是两种不同理念的前端工具,jQuery 以 DOM 操作和事件驱动为核心,而 Vue.js 是数据驱动的现代化框架。如果想用 jQuery 实现类似 Vue 的功能(如…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

javascript实现vue

javascript实现vue

Vue.js 的基本实现 在 JavaScript 中实现 Vue.js 的核心功能,主要包括数据绑定、响应式系统和虚拟 DOM。以下是一个简化的实现示例。 数据响应式系统 通过 Object.de…

vue 实现跳转

vue 实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式实现,具体选择取决于项目需求和技术栈(如是否使用 Vue Router)。 使用 Vue Router 进行编程式导航…

mvvm实现vue

mvvm实现vue

MVVM 模式在 Vue 中的实现 MVVM(Model-View-ViewModel)是一种前端架构模式,Vue 的设计灵感来源于此。以下是 MVVM 在 Vue 中的核心实现方式: 数据绑定(M…