当前位置:首页 > 技术 > 正文内容

基于 Vite 与 Vue 构建支持 SEO 的单页应用

访客 技术 2026年7月7日 1

项目初始化

通过 Vite 快速搭建 Vue 项目基础结构:

npm create vite@latest seo-friendly-spa --template vue
cd seo-friendly-spa
npm install

安装核心依赖与 SEO 工具

npm install vue-router pinia
npm install @vueuse/head vite-plugin-html
npm install prerender-spa-plugin -D

配置构建环境

更新 vite.config.js 文件以集成 HTML 注入与元数据管理:

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

export default defineConfig({
  plugins: [
    vue(),
    createHtmlPlugin({
      minify: true,
      inject: {
        data: {
          title: 'SEO 友好型 Vue 单页应用',
          description: '使用 Vite + Vue 构建的可搜索引擎优化的前端应用'
        }
      }
    })
  ]
})

路由系统设置

src/router/index.js 中定义页面导航规则:

import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: HomeView,
    meta: {
      title: '首页 | 网站名称',
      description: '展示最新动态与服务内容'
    }
  },
  {
    path: '/about',
    name: 'About',
    component: AboutView,
    meta: {
      title: '关于团队 | 网站名称',
      description: '深入了解我们的发展历程与核心成员'
    }
  }
]

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

export default router

统一管理页面元信息

创建 src/utils/metaManager.js 来集中处理标题与描述:

import { useHead } from '@vueuse/head'

export const setMetaTags = (metaConfig) => {
  useHead({
    title: metaConfig.title,
    meta: [
      { name: 'description', content: metaConfig.description },
      { property: 'og:title', content: metaConfig.title },
      { property: 'og:description', content: metaConfig.description },
      { property: 'og:type', content: 'website' },
      { name: 'twitter:card', content: 'summary_large_image' },
      { name: 'twitter:title', content: metaConfig.title },
      { name: 'twitter:description', content: metaConfig.description }
    ]
  })
}

主应用入口配置

修改 src/main.js,引入状态管理、路由和头部管理器:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import { createHead } from '@vueuse/head'

const app = createApp(App)
const headManager = createHead()

app.use(createPinia())
app.use(router)
app.use(headManager)

app.mount('#app')

页面组件开发

实现两个典型页面:首页与关于我们。

src/views/HomeView.vue

<template>
  <section class="home-page">
    <h1>欢迎访问我们的网站</h1>
    <p>本应用采用现代前端架构,兼顾性能与可搜索性。</p>
    <router-link to="/about">了解更多详情</router-link>
  </section>
</template>

<script>
import { setMetaTags } from '@/utils/metaManager'

export default {
  name: 'HomeView',
  setup() {
    setMetaTags({
      title: '首页 | 网站名称',
      description: '展示最新产品与服务信息'
    })
  }
}
</script>

src/views/AboutView.vue

<template>
  <section class="about-page">
    <h1>关于我们的团队</h1>
    <p>我们专注于高效、可维护的前端解决方案。</p>
    <router-link to="/">返回首页</router-link>
  </section>
</template>

<script>
import { setMetaTags } from '@/utils/metaManager'

export default {
  name: 'AboutView',
  setup() {
    setMetaTags({
      title: '关于团队 | 网站名称',
      description: '介绍公司背景与核心成员构成'
    })
  }
}
</script>

主视图整合

<template>
  <header>
    <nav>
      <router-link to="/">首页</router-link> |
      <router-link to="/about">关于</router-link>
    </nav>
  </header>
  <main>
    <router-view />
  </main>
</template>

<style>
#app {
  font-family: 'Segoe UI', sans-serif;
  text-align: center;
  color: #333;
  margin-top: 50px;
}

nav a {
  margin: 0 15px;
  font-weight: 500;
  color: #4a5568;
  text-decoration: none;
}

nav a.router-link-active {
  color: #38bdf8;
  border-bottom: 2px solid #38bdf8;
}
</style>

技术选型说明

  • Vite:提供极速冷启动与热更新能力
  • Vue 3:响应式系统与组合式 API 支持
  • Vue Router:声明式路由机制
  • Pinia:轻量级状态管理方案
  • @vueuse/head:运行时动态控制页面 <head> 内容
  • vite-plugin-html:支持模板注入与变量替换

SEO 优化策略

  • 每个路由独立设置标题与描述
  • 集成 Open Graph 与 Twitter 卡片标签
  • 支持预渲染(通过 prerender-spa-plugin
  • 使用语义化标签提升可读性
  • 为未来迁移到 SSR 做好结构准备

项目目录结构

seo-friendly-spa/
├── public/
├── src/
│   ├── views/
│   │   ├── HomeView.vue
│   │   └── AboutView.vue
│   ├── utils/
│   │   └── metaManager.js
│   ├── router/
│   │   └── index.js
│   ├── App.vue
│   └── main.js
├── vite.config.js
└── package.json

启动项目

npm run dev

相关文章

Linux crontab 详解

1) crontab 是什么cron 是 Linux 的定时任务守护进程;crontab 是用来编辑/查看“按时间周期执行命令”的表(cron table)。常见两类:用户 crontab:每个用户一份(crontab -e 编辑)系统级 crontab / cron.d:可指定执行用户(/etc/crontab、/etc/cron.d/*)2) crontab 时间...

富文本里可以允许的 HTML 属性

一、所有标签默认允许的安全属性(极少)class        (可选)id           (通常建议禁用)title️ 注意:id 容易被滥用做锚点注入,很多系统直接禁用class 允许的话最好只允许固定前缀(如 editor-*)二、a 标签允许属性<a href="" t...

Mac 安装 Node.js 指南

方法一:通过官网安装包(最简单,适合初学者)如果你只是想快速安装并开始使用,这是最直接的方法。访问 Node.js 官网。页面会显示两个版本:LTS (Recommended For Most Users):长期支持版,最稳定。建议选这个。Current:最新特性版,包含最新功能但可能不够稳定。下载 .pkg 安装包并运行。按照安装向导点击“下一步”即可完成。方法二:使用 Homebrew 安装(...

Dom\HTML_NO_DEFAULT_NS 的副作用:自动加闭合标签

在使用Dom\HTMLDocument时,Dom\HTML_NO_DEFAULT_NS 将禁止在解析过程中设置元素的命名空间, 此设置是为了与DOMDocument向后兼容而存在的。当使用它时,已知的一个副作用就是:自动加闭合标签例如 </img> 为什么会这样?当你使用:Dom\HTML_NO_DEFAULT_NS文档会变成 无命名空间模式,此时内部更接近 XML...

Laravel 事件和监听器创建

在 Laravel 中,使用 Artisan 命令创建 Events(事件) 和 Listeners(监听器) 是非常高效的。你可以通过以下几种方式来实现:1. 手动创建单个 Event如果你只想创建一个事件类,可以使用 make:event 命令:Bashphp artisan make:event UserRegistered执行后,文件将生成在 app/Even...

自定义域名解析神器 dnsmasq

什么是 dnsmasq?dnsmasq 是一个轻量级、功能强大的网络服务工具,专为小型和中等规模网络设计。它是一个综合的网络基础设施解决方案[1]。dnsmasq 能做什么?功能说明应用场景DNS 转发与缓存将 DNS 查询转发到上游服务器(ISP、Google DNS 等),并在本地缓存结果加快 DNS 查询速度,减少外部 DNS 流量本地 DNS解析本地网络设备的主机名,无需编辑&n...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。