当前位置:首页 > VUE

vue实现博客

2026-02-10 05:03:37VUE

使用 Vue 实现博客的关键步骤

项目初始化与配置

使用 Vue CLI 或 Vite 创建项目,安装必要依赖:

npm create vue@latest vue-blog
cd vue-blog
npm install vue-router pinia axios

配置路由文件(src/router/index.js):

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

const routes = [
  { path: '/', component: Home },
  { path: '/post/:id', component: () => import('../views/PostDetail.vue') }
]

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

状态管理与API交互

创建 Pinia store(src/stores/posts.js)管理博客数据:

import { defineStore } from 'pinia'
import axios from 'axios'

export const usePostsStore = defineStore('posts', {
  state: () => ({
    posts: [],
    currentPost: null
  }),
  actions: {
    async fetchPosts() {
      const res = await axios.get('https://api.example.com/posts')
      this.posts = res.data
    },
    async fetchPost(id) {
      const res = await axios.get(`https://api.example.com/posts/${id}`)
      this.currentPost = res.data
    }
  }
})

博客列表组件

创建BlogList.vue组件显示文章列表:

<template>
  <div v-for="post in posts" :key="post.id">
    <router-link :to="'/post/' + post.id">
      <h3>{{ post.title }}</h3>
    </router-link>
    <p>{{ post.excerpt }}</p>
  </div>
</template>

<script setup>
import { usePostsStore } from '../stores/posts'
import { storeToRefs } from 'pinia'

const store = usePostsStore()
const { posts } = storeToRefs(store)
store.fetchPosts()
</script>

文章详情页

创建PostDetail.vue组件显示单篇文章:

vue实现博客

<template>
  <article v-if="post">
    <h1>{{ post.title }}</h1>
    <div v-html="post.content"></div>
  </article>
</template>

<script setup>
import { useRoute } from 'vue-router'
import { usePostsStore } from '../stores/posts'

const route = useRoute()
const store = usePostsStore()
store.fetchPost(route.params.id)

const post = computed(() => store.currentPost)
</script>

样式与布局优化

添加基础样式(src/assets/main.css):

.blog-post {
  max-width: 800px;
  margin: 0 auto;
  padding: 2rem;
}

.post-list {
  display: grid;
  gap: 1.5rem;
  padding: 2rem;
}

部署准备

配置生产环境变量(.env.production):

VITE_API_BASE_URL=https://production-api.example.com

构建项目:

vue实现博客

npm run build

扩展功能建议

用户认证系统

集成 Firebase Auth 或 JWT 实现登录功能:

// 在Pinia store中添加
async login(email, password) {
  const res = await axios.post('/auth/login', { email, password })
  localStorage.setItem('token', res.data.token)
}

评论功能

创建评论组件:

<template>
  <div v-for="comment in comments" :key="comment.id">
    <p>{{ comment.author }}: {{ comment.content }}</p>
  </div>
</template>

Markdown支持

安装marked库解析Markdown内容:

npm install marked

使用示例:

import { marked } from 'marked'
const htmlContent = marked(markdownText)

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

相关文章

vue实现适老化样式

vue实现适老化样式

Vue 实现适老化样式的关键方法 全局字体与字号调整 通过 CSS 变量或主题配置统一放大基础字号,建议正文不小于 18px,标题更大。在 App.vue 中设置全局样式: :root { --…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现画廊

vue实现画廊

Vue实现画廊的基本方法 使用Vue.js实现画廊功能可以通过多种方式完成,以下是一种常见的实现方案: 安装必要依赖 npm install vue-gallery --save 引入组件并注册…

vue实现刷卡

vue实现刷卡

Vue 实现刷卡效果 在 Vue 中实现刷卡效果可以通过 CSS 动画和 Vue 的过渡系统结合完成。以下是一种常见的实现方式: 基础实现方法 安装 Vue 过渡依赖(如果尚未安装): npm i…

vue实现书库

vue实现书库

Vue 实现书库功能 使用 Vue 构建书库功能需要结合前端框架和后端数据管理。以下是实现步骤和关键点: 项目初始化 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 和状…

vue实现加载

vue实现加载

Vue 实现加载功能的方法 使用 v-if 或 v-show 控制加载状态 通过数据绑定控制加载动画的显示与隐藏。v-if 会完全移除 DOM 元素,v-show 只是切换 display 属性。…