当前位置:首页 > 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。

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

相关文章

css制作博客

css制作博客

CSS制作博客的关键步骤 设计布局结构 博客通常采用经典的三栏布局:头部导航、主内容区和侧边栏。使用Flexbox或Grid布局实现响应式设计,确保在不同设备上正常显示。 .blog-contain…

博客css制作

博客css制作

CSS 制作博客的实用方法 布局设计 博客布局通常采用响应式设计,确保在不同设备上都能良好显示。常见的布局结构包括头部(导航栏)、主体内容区(文章列表或单篇文章)和页脚。 使用 CSS Grid…

使用php实现博客

使用php实现博客

使用PHP实现博客的基本步骤 环境准备 安装PHP运行环境(如XAMPP、WAMP或LAMP),确保包含MySQL数据库支持。推荐使用PHP 7.4及以上版本,搭配Apache/Nginx服务器。…

jquery博客

jquery博客

jQuery 博客开发指南 基础环境搭建 确保项目中引入 jQuery 库,可通过 CDN 或本地文件加载: <script src="https://code.jquery.com/jq…

vue实现博客

vue实现博客

使用 Vue 实现博客的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装必要依赖: npm create vue@latest vue-blog cd vue-blog…

使用php实现博客

使用php实现博客

使用PHP实现博客的基本步骤 数据库设计 创建MySQL数据库表存储博客内容,例如posts表包含id、title、content、created_at等字段。用户表users可包含id、userna…