当前位置:首页 > VUE

vue实现个人博客

2026-03-09 16:19:12VUE

Vue 实现个人博客的步骤

项目初始化与基础配置

使用 Vue CLI 或 Vite 创建项目,安装基础依赖:

npm init vue@latest vue-blog
cd vue-blog
npm install

安装路由和状态管理库:

npm install vue-router pinia

路由配置

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
})

export default router

文章列表与详情页实现

创建文章列表组件 src/components/PostList.vue

<template>
  <div v-for="post in posts" :key="post.id">
    <h3 @click="navigateToPost(post.id)">{{ post.title }}</h3>
    <p>{{ post.excerpt }}</p>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
const router = useRouter()

const posts = [
  { id: 1, title: 'Vue入门', excerpt: '学习Vue基础知识' },
  { id: 2, title: 'Pinia使用', excerpt: '状态管理解决方案' }
]

const navigateToPost = (id) => {
  router.push(`/post/${id}`)
}
</script>

状态管理

使用 Pinia 管理全局状态,创建 src/stores/blog.js

import { defineStore } from 'pinia'

export const useBlogStore = defineStore('blog', {
  state: () => ({
    posts: [],
    currentPost: null
  }),
  actions: {
    async fetchPosts() {
      // 这里可以替换为API调用
      this.posts = [
        { id: 1, title: 'Vue入门', content: '...' },
        { id: 2, title: 'Pinia使用', content: '...' }
      ]
    }
  }
})

样式与布局

添加基础样式文件 src/assets/main.css

body {
  font-family: 'Arial', sans-serif;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

部署准备

配置 vite.config.js 用于生产环境构建:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  base: '/blog/'
})

进阶功能扩展

  1. 添加Markdown支持:
    npm install marked highlight.js

    创建Markdown渲染组件 src/components/MarkdownRenderer.vue

    
    <template>
    <div v-html="compiledMarkdown"></div>
    </template>
import { marked } from 'marked' import hljs from 'highlight.js'

marked.setOptions({ highlight: (code) => hljs.highlightAuto(code).value })

const props = defineProps(['content']) const compiledMarkdown = computed(() => marked(props.content))

```
  1. 实现评论功能:

    npm install firebase

    配置Firebase实时数据库存储和显示评论。

  2. 添加SEO优化:

    npm install vue-meta

    在入口文件中配置:

    
    import { createMetaManager } from 'vue-meta'

app.use(createMetaManager())


#### 项目结构建议

src/ ├── assets/ ├── components/ │ ├── PostList.vue │ └── MarkdownRenderer.vue ├── stores/ │ └── blog.js ├── views/ │ ├── Home.vue │ └── PostDetail.vue ├── router/ │ └── index.js └── App.vue

vue实现个人博客


#### 部署选项
1. 静态托管服务:
- Netlify
- Vercel
- GitHub Pages

2. 服务器部署:
- 构建生产版本:`npm run build`
- 上传 `dist` 目录到服务器

3. Docker部署:
创建 `Dockerfile`:
```dockerfile
FROM node:16 as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=build-stage /app/dist /usr/share/nginx/html

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

相关文章

vue实现发表博客

vue实现发表博客

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

css制作博客

css制作博客

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

uniapp 博客框架

uniapp 博客框架

uniapp 博客框架推荐 uniapp 是一个基于 Vue.js 的跨平台开发框架,适合快速构建博客类应用。以下是几种常见的 uniapp 博客框架或解决方案: 1. uni-app 官方模板 u…

vue实现静态博客

vue实现静态博客

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

vue实现博客

vue实现博客

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

博客css制作

博客css制作

博客CSS制作方法 基础样式设置 使用CSS为博客设置统一的字体、颜色和间距。例如: body { font-family: 'Arial', sans-serif; line-heig…