ECC证书与SAP ECC LSMW:从P-384信任链到数据迁移实操
2026/10/28 12:07:54
SPA(Single Page Application,单页应用)是一种 Web 应用架构模型。它在初始加载时只请求一次 HTML 页面,后续的页面切换完全由 JavaScript 动态完成——通过操作 DOM 或组件替换内容,无需整页刷新,从而提供接近原生 App 的流畅体验。
举个形象的例子:
就像一个杯子,早上装牛奶,中午装开水,晚上泡茶——容器(HTML 结构)始终不变,变的只是内容(视图/组件)。
目前主流的前端框架如Vue、React、Angular、Ember等,都是围绕 SPA 模式设计的。
| 对比维度 | SPA(单页应用) | MPA(多页应用) |
|---|---|---|
| 组成 | 1 个主 HTML + 多个动态组件/片段 | 多个独立 HTML 页面 |
| 刷新方式 | 局部更新(无整页刷新) | 每次跳转都重新加载页面 |
| URL 模式 | /#/home(Hash)或/home(History) | /home.html、/about.html等 |
| SEO 友好度 | 默认不友好(内容由 JS 渲染) | 天然友好(HTML 直出) |
| 数据传递 | 组件间直接通信(如 props / Vuex) | 依赖 URL 参数、Cookie、localStorage 等 |
| 用户体验 | 切换快、无白屏、交互流畅 | 跳转慢、有加载等待 |
| 维护成本 | 组件化,逻辑清晰,易维护 | 页面重复代码多,耦合高 |
SPA 的核心是前端路由(Client-side Routing),主要有两种实现方式:
#)#后的内容变化(如http://example.com/#/home);hashchange事件实现视图切换。// 定义 RouterclassRouter{constructor(){this.routes={};// 存放路由 path 及对应的回调函数this.currentUrl='';// 页面加载时初始化window.addEventListener('load',this.refresh.bind(this),false);// 监听 hash 变化window.addEventListener('hashchange',this.refresh.bind(this),false);}route(path,callback){this.routes[path]=callback;}refresh(){this.currentUrl=location.hash.slice(1)||'/';this.routes[this.currentUrl]&&this.routes[this.currentUrl]();}push(path){location.hash=path;}}// 使用示例window.miniRouter=newRouter();miniRouter.route('/',()=>console.log('首页'));miniRouter.route('/page2',()=>console.log('页面2'));miniRouter.push('/');// 输出:首页miniRouter.push('/page2');// 输出:页面2✅ 优点:兼容性好,适用于所有浏览器。
❌ 缺点:URL 不够美观,部分平台(如微信)对#有特殊限制。
history.pushState()和history.replaceState()修改 URL;http://example.com/home);index.html,否则刷新会 404;popstate事件处理浏览器前进/后退。// 定义 RouterclassRouter{constructor(){this.routes={};this.listenPopState();}init(path){history.replaceState({path},null,path);this.routes[path]&&this.routes[path]();}route(path,callback){this.routes[path]=callback;}push(path){history.pushState({path},null,path);this.routes[path]&&this.routes[path]();}listenPopState(){window.addEventListener('popstate',(e)=>{constpath=e.state&&e.state.path;this.routes[path]&&this.routes[path]();});}}// 使用示例window.miniRouter=newRouter();miniRouter.route('/',()=>console.log('首页'));miniRouter.route('/page2',()=>console.log('页面2'));miniRouter.init('/');// 初始化首页miniRouter.push('/page2');// 跳转到页面2✅ 优点:URL 美观,符合语义化;
❌ 缺点:需要服务端配置支持,否则刷新页面会 404。
虽然 SPA 天然不利于搜索引擎抓取,但有以下成熟方案:
prerender-spa-plugin、Vite 插件等。SPA 是现代 Web 开发的主流范式,它以极致的用户体验为核心,但也带来了首屏性能与SEO的挑战。
在实际项目中,我们应根据业务需求选择合适的路由模式(Hash / History),并通过SSR、预渲染或动态渲染等手段弥补其短板,实现性能、体验与可访问性的平衡。
参考文献
- 掘金:单页面 vs 多页面
- 博客园:如何快速开发 SPA 应用
- SegmentFault:SPA 原理与实现