当前位置:首页 > VUE

vue实现个人博客

2026-01-17 10:56:07VUE

使用 Vue 实现个人博客的步骤

项目初始化

通过 Vue CLI 或 Vite 创建一个新的 Vue 项目。Vue 3 推荐使用 Vite,运行以下命令:

npm create vite@latest my-blog --template vue
cd my-blog
npm install

安装必要的依赖,如路由管理和状态管理:

npm install vue-router pinia

路由配置

src/router/index.js 中配置路由,定义博客的首页、文章详情页等路由:

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

const routes = [
  { path: '/', component: Home },
  { path: '/post/:id', component: Post }
]

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

export default router

博客文章管理

使用 Pinia 管理博客文章的状态。创建 src/stores/postStore.js

vue实现个人博客

import { defineStore } from 'pinia'

export const usePostStore = defineStore('post', {
  state: () => ({
    posts: [
      { id: 1, title: 'Vue 入门', content: 'Vue 是一个渐进式框架...' },
      { id: 2, title: 'Pinia 使用', content: 'Pinia 是 Vue 的状态管理工具...' }
    ]
  }),
  getters: {
    getPostById: (state) => (id) => state.posts.find(post => post.id === id)
  }
})

文章列表展示

Home.vue 中展示文章列表:

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

<script setup>
import { usePostStore } from '@/stores/postStore'
const postStore = usePostStore()
</script>

文章详情页

Post.vue 中根据路由参数显示文章内容:

<template>
  <h1>{{ post.title }}</h1>
  <p>{{ post.content }}</p>
</template>

<script setup>
import { useRoute } from 'vue-router'
import { usePostStore } from '@/stores/postStore'

const route = useRoute()
const postStore = usePostStore()
const post = postStore.getPostById(Number(route.params.id))
</script>

样式设计

可以使用 CSS 预处理器如 SCSS 或直接使用 Tailwind CSS 来美化博客界面。安装 Tailwind CSS:

vue实现个人博客

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init

配置 tailwind.config.js

module.exports = {
  content: ['./index.html', './src//*.{vue,js,ts,jsx,tsx}'],
  theme: {
    extend: {},
  },
  plugins: [],
}

src/assets 目录下添加全局样式文件,并在 main.js 中引入。

部署上线

项目开发完成后,构建生产环境代码:

npm run build

生成的 dist 文件夹可以部署到任何静态网站托管服务,如 Vercel、Netlify 或 GitHub Pages。

标签: 个人博客
分享给朋友:

相关文章

vue实现发表博客

vue实现发表博客

Vue 实现发表博客功能 创建 Vue 项目 使用 Vue CLI 创建一个新项目,确保已安装 Node.js 和 npm/yarn。运行以下命令初始化项目: vue create blog-app…

vue node实现个人博客

vue node实现个人博客

Vue + Node.js 实现个人博客的关键步骤 技术栈选择 前端:Vue 3 + Vite + Pinia(状态管理) 后端:Node.js + Express/Koa + MongoDB/My…