当前位置:首页 > 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 构建静态博客可以通过多种方式实现,以下是几种常见的方法: 使用 VuePress VuePress 是 Vue 官方推出的静态网站生成器,适合技术文档…

vue博客实现

vue博客实现

Vue 博客实现步骤 项目初始化与基础配置 使用 Vue CLI 或 Vite 创建项目,安装基础依赖如 vue-router 和状态管理库(如 pinia)。配置路由文件实现页面跳转逻辑,例如博客首…

vue实现静态博客

vue实现静态博客

Vue 实现静态博客的方法 使用 Vue 实现静态博客可以通过多种方式完成,以下是几种常见的方法: 使用 VuePress VuePress 是一个由 Vue 驱动的静态网站生成器,特别适合技术文档…

php实现博客

php实现博客

使用PHP实现博客系统 数据库设计 创建数据库表存储博客文章和用户信息。常见的表包括posts(文章)和users(用户)。 CREATE TABLE users ( id INT AUT…

php实现博客系统

php实现博客系统

数据库设计 创建MySQL数据库表存储博客内容。主要需要posts表(文章)、categories表(分类)、users表(用户)和comments表(评论)。以下是核心表的SQL示例: CREA…

vue实现发表博客

vue实现发表博客

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