当前位置:首页 > 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组件显示单篇文章:

<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

构建项目:

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实现页面截屏

使用html2canvas库实现截屏 html2canvas是一个流行的JavaScript库,可以将DOM元素转换为Canvas图像。在Vue项目中安装html2canvas: npm inst…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue实现拍照

vue实现拍照

Vue实现拍照功能 在Vue中实现拍照功能通常需要结合浏览器的MediaDevices API和Canvas元素。以下是具体实现方法: 安装依赖(可选) 如果需要更简单的封装,可以安装vue-w…

vue实现标签

vue实现标签

Vue 实现标签的方法 在 Vue 中实现标签功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 动态组件可以根据不同的条件渲染不同的标签内容。通过 :is 属性动态绑定组件名,实现标签…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定…