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

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:

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 中引入。

部署上线

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

vue实现个人博客

npm run build

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

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

相关文章

使用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 实现静态博客可以通过多种方式完成,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态网站生成器,非常适合构建博客或…

vue博客实现

vue博客实现

搭建基础项目结构 使用 Vue CLI 快速初始化项目,确保已安装 Node.js 和 npm/yarn。执行以下命令创建项目: vue create vue-blog 选择默认配置或手动配置(推荐…

css制作博客

css制作博客

CSS 制作博客的关键步骤 布局设计 使用 display: flex 或 display: grid 构建响应式布局,主容器设置 max-width: 1200px 并居中。侧边栏宽度建议 25%,…

css博客制作

css博客制作

CSS博客制作指南 制作一个CSS博客需要结合HTML结构、CSS样式设计以及可能的JavaScript交互。以下是关键步骤和技巧: 基础结构搭建 使用HTML5语义化标签构建博客框架,如<h…