lazyload
search
Search
span n>
首页
Blog
Code
Literature
More
language
lazyload
PHODAL
Home
Blog
Literature
《自己动手设计物联网》
《全栈应用开发:精益实践》
《前端架构:从入门到微前端》
查看标签 lazyload
适用于 Angular Lazyload 下的 RouteReuseStrategy
作者:
Phodal Huang
2018年5月12日 09:44
之前做过一个 Angular 下的路由重用,即记录 SPA 应用的上一个页面的内容。最开始的时候,并没有考虑 Lazyload,因此实现起来就比较简单: ``` export class SimpleReuseStrategy implements RouteReuseStrategy { public static handlers: { [key: string]: DetachedRouteHandle } = {}; public shouldDetach(route: ActivatedRouteSnapshot): boolean { return true; } public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { SimpleReuseStrategy.handlers[route.routeConfig.path] = handle; } public shouldAttach(route: ActivatedRouteSnapshot): boolean { return !!route.routeConfig && !!SimpleReuseStrategy.handlers[route.routeConfig.path]; } public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { if (!route.routeConfig) { return null; } return SimpleReuseStrategy.handlers[route.routeConfig.path]; } public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { return future.routeConfig === curr.routeConfig; } } ``` 后来项目中添加了 Lazyload,因此便添加了一个 reusePath 的参数来控制是否缓存,最后实现如下: ``` export class SimpleReuseStrategy implements RouteReuseStrategy { public static handlers: { [key: string]: DetachedRouteHandle } = {}; public shouldDetach(route: ActivatedRouteSnapshot): boolean { if (!!route.data && !!route.data.reusePath) { return true; } return false; } /** 当路由离开时会触发。按reusePath作为key存储路由快照&组件当前实例对象 */ public store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void { SimpleReuseStrategy.handlers[route.data.reusePath] = handle; } /** 若 path 在缓存中有的都认为允许还原路由 */ public shouldAttach(route: ActivatedRouteSnapshot): boolean { if (!!route.data.reusePath && !!SimpleReuseStrategy.handlers[route.data.reusePath]) { return true; } return false; } /** 从缓存中获取快照,若无则返回nul */ public retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle { if (!!route.data && !!route.data.reusePath) { return SimpleReuseStrategy.handlers[route.data.reusePath]; } return null; } /** 进入路由触发,判断是否同一路由 */ public shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean { if (future.data && curr.data && future.data.reusePath && curr.data.reusePath) { return future.data.reusePath === curr.data.reusePath; } return false; } } ``` 为了保存路由,需要单独额外的 在 Routing 里 添加一个 key : ``` const routes: Routes = [ { data: { reusePath: 'hello/hello' }, path: 'hello/hello', loadChildren: './hello/hello.module#CheckinModule' } ] ```
标签:
angular
lazyload
spa
更多
arrow_forward
Feeds
RSS
/
Atom
最近文章
Harness Engineering Skill:使用 Entrix 技能开始你的代码熵治理
从 0 到 25 万行:一个 100% AI 编码的项目如何建立治理闭环
Entrix:我们如何用反熵机制治理 Vibe Coding
AI 编码 3.0:当 Agent 协作开始被系统执行
Harness Engineering 的下一步:Fitness Function 定义 AI Agent 的完成条件
当 Kanban 不再管理人:Routa Kanban 如何管理 Agent Team
Harness Engineering 的防御视角:从 Codex Security 看 AI 生成代码的治理
AI Coding Fluency:从工具使用到人机协作的软件工程
Harness Engineering 实践指南:落地探索的三大原则
2026 年,万物皆 Coding Agent 的平台工程(A2A / ACP / MCP / Skill)
存档
▶
2026
(3 个月)
三月
(11)
二月
(4)
一月
(2)
▶
2025
(12 个月)
十二月
(5)
十一月
(7)
十月
(1)
九月
(2)
八月
(2)
七月
(2)
六月
(1)
五月
(7)
四月
(6)
三月
(6)
二月
(1)
一月
(2)
▶
2024
(12 个月)
十二月
(3)
十一月
(3)
十月
(3)
九月
(5)
八月
(4)
七月
(4)
六月
(1)
五月
(3)
四月
(2)
三月
(5)
二月
(2)
一月
(6)
▶
2023
(12 个月)
十二月
(6)
十一月
(4)
十月
(5)
九月
(4)
八月
(4)
七月
(5)
六月
(7)
五月
(4)
四月
(3)
三月
(9)
二月
(6)
一月
(2)
▶
2022
(12 个月)
十二月
(2)
十一月
(3)
十月
(3)
九月
(2)
八月
(5)
七月
(4)
六月
(3)
五月
(5)
四月
(1)
三月
(2)
二月
(3)
一月
(1)
▶
2021
(12 个月)
十二月
(6)
十一月
(5)
十月
(2)
九月
(4)
八月
(4)
七月
(2)
六月
(4)
五月
(4)
四月
(3)
三月
(4)
二月
(2)
一月
(3)
▶
2020
(12 个月)
十二月
(7)
十一月
(7)
十月
(6)
九月
(7)
八月
(9)
七月
(5)
六月
(5)
五月
(5)
四月
(8)
三月
(5)
二月
(5)
一月
(3)
▶
2019
(12 个月)
十二月
(6)
十一月
(4)
十月
(4)
九月
(5)
八月
(4)
七月
(10)
六月
(3)
五月
(6)
四月
(5)
三月
(7)
二月
(4)
一月
(3)
▶
2018
(12 个月)
十二月
(4)
十一月
(5)
十月
(4)
九月
(3)
八月
(5)
七月
(6)
六月
(3)
五月
(5)
四月
(4)
三月
(5)
二月
(2)
一月
(7)
▶
2017
(12 个月)
十二月
(21)
十一月
(19)
十月
(9)
九月
(4)
八月
(5)
七月
(4)
六月
(5)
五月
(4)
四月
(2)
三月
(3)
二月
(1)
一月
(1)
▶
2016
(12 个月)
十二月
(7)
十一月
(5)
十月
(1)
九月
(2)
八月
(2)
七月
(4)
六月
(4)
五月
(7)
四月
(3)
三月
(7)
二月
(8)
一月
(7)
▶
2015
(12 个月)
十二月
(10)
十一月
(10)
十月
(5)
九月
(10)
八月
(12)
七月
(4)
六月
(7)
五月
(6)
四月
(21)
三月
(9)
二月
(8)
一月
(25)
▶
2014
(12 个月)
十二月
(22)
十一月
(16)
十月
(15)
九月
(14)
八月
(30)
七月
(30)
六月
(12)
五月
(47)
四月
(49)
三月
(29)
二月
(12)
一月
(24)
▶
2013
(9 个月)
十二月
(29)
十一月
(9)
十月
(3)
七月
(4)
六月
(1)
五月
(3)
三月
(17)
二月
(13)
一月
(7)
▶
2012
(3 个月)
十二月
(8)
十一月
(3)
三月
(1)
▶
2011
(1 月)
十月
(1)
▶
2010
(1 月)
十二月
(1)
▶
1991
(1 月)
六月
(1)
展开全部
分类
mac os
(10)
Technology
(11)
Thoughtworks
(14)
Internet Of Things
(41)
Share
(26)
ThinkZone
(37)
Arduino
(9)
Hardware
(30)
Notes
(13)
Pythoner Learn Ruby
(5)
Think Of REWORK
(11)
Android
(27)
OpenSUSE
(6)
Linux
(20)
SEO
(9)
Quick Emacs
(6)
Full Stack
(131)
be-a-geek
(11)
Machine learning
(9)
microservices
(22)
Javascript
(55)
Mobile CMS
(11)
CoAP
(18)
Nodejs
(7)
AWS
(8)
杂谈
(100)
翻译
(7)
Build JavaScript FrameWork
(11)
DSL
(8)
Ionic
(13)
Geo
(7)
GIS
(5)
ReThink
(5)
Hybird
(6)
Architecture
(21)
APP
(11)
serverless
(24)
架构拾集
(7)
架构
(21)
AI
(114)
标签
opensuse
(10)
django
(41)
arduino
(10)
thoughtworks
(18)
centos
(9)
nginx
(18)
java
(10)
SEO
(9)
iot
(47)
iot system
(12)
RESTful
(23)
refactor
(17)
python
(47)
mezzanine
(15)
test
(11)
design
(16)
linux
(14)
tdd
(12)
ruby
(14)
github
(24)
git
(10)
javascript
(52)
android
(36)
jquery
(18)
rework
(13)
markdown
(10)
nodejs
(24)
google
(8)
code
(9)
macos
(9)
node
(11)
think
(8)
beageek
(8)
underscore
(14)
ux
(8)
microservices
(10)
rethink
(9)
architecture
(37)
backbone
(19)
mustache
(9)
requirejs
(11)
CoAP
(21)
aws
(10)
dsl
(9)
ionic
(25)
Cordova
(21)
angular
(16)
react
(14)
ddd
(9)
summary
(9)
growth
(10)
frontend
(14)
react native
(8)
serverless
(32)
rust
(9)
llm
(8)
作者
Phodal Huang
(1085)