Angular路由复用策略RouteReuseStrategy
<p><strong>第一步</strong></p><p id="item-2-1"><strong>新建RouteReuseStrategy</strong></p>
<p>新建一个<code>CustomReuseStrategy.ts</code> 实现接口 RouteReuseStrategy</p>
<pre class="typescript hljs"><code class="typescript"><span class="hljs-keyword">import { RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle } <span class="hljs-keyword">from <span class="hljs-string">'@angular/router';
<span class="hljs-keyword">export <span class="hljs-keyword">class CustomReuseStrategy <span class="hljs-keyword">implements RouteReuseStrategy {
<span class="hljs-keyword">public <span class="hljs-keyword">static handlers: { : DetachedRouteHandle } = {};
<span class="hljs-comment">/** 删除缓存路由快照的方法 */
<span class="hljs-keyword">public <span class="hljs-keyword">static deleteRouteSnapshot(path: <span class="hljs-built_in">string): <span class="hljs-built_in">void {
<span class="hljs-keyword">const name = path.replace(<span class="hljs-regexp">/\//g, <span class="hljs-string">'_');
<span class="hljs-keyword">if (CustomReuseStrategy.handlers) {
<span class="hljs-keyword">delete CustomReuseStrategy.handlers;
}
}
<span class="hljs-comment">/** 表示对所有路由允许复用 如果你有路由不想利用可以在这加一些业务逻辑判断 */
shouldDetach(route: ActivatedRouteSnapshot): <span class="hljs-built_in">boolean {
<span class="hljs-comment">// console.debug('shouldDetach======>', route);<br> return route.data.relad || false;
// <span class="hljs-keyword">return <span class="hljs-literal">true;
}
<span class="hljs-comment">/** 当路由离开时会触发。按path作为key存储路由快照&组件当前实例对象 */
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): <span class="hljs-built_in">void {
<span class="hljs-comment">// console.debug('store======>', route, handle);
CustomReuseStrategy.handlers[<span class="hljs-keyword">this.getRouteUrl(route)] = handle;
}
<span class="hljs-comment">/** 若 path 在缓存中有的都认为允许还原路由 */
shouldAttach(route: ActivatedRouteSnapshot): <span class="hljs-built_in">boolean {
<span class="hljs-comment">// console.debug('shouldAttach======>', route);
<span class="hljs-keyword">return !!CustomReuseStrategy.handlers[<span class="hljs-keyword">this.getRouteUrl(route)];
}
<span class="hljs-comment">/** 从缓存中获取快照,若无则返回nul */
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
<span class="hljs-comment">// console.debug('retrieve======>', route);
<span class="hljs-keyword">if (!CustomReuseStrategy.handlers[<span class="hljs-keyword">this.getRouteUrl(route)]) {
<span class="hljs-keyword">return <span class="hljs-literal">null;
}
<span class="hljs-keyword">return CustomReuseStrategy.handlers[<span class="hljs-keyword">this.getRouteUrl(route)];
}
<span class="hljs-comment">/** 进入路由触发,判断是否同一路由 */
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): <span class="hljs-built_in">boolean {
<span class="hljs-comment">// console.debug('shouldReuseRoute======>', future, curr);
<span class="hljs-keyword">return future.routeConfig === curr.routeConfig &&
<span class="hljs-built_in">JSON.stringify(future.params) === <span class="hljs-built_in">JSON.stringify(curr.params);
}
<span class="hljs-comment">/** 使用route的path作为快照的key */
getRouteUrl(route: ActivatedRouteSnapshot) {
<span class="hljs-keyword">const path = route[<span class="hljs-string">'_routerState'].url.replace(<span class="hljs-regexp">/\//g, <span class="hljs-string">'_');
<span class="hljs-keyword">return path;
}
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code><br><br><strong>第二步:</strong><br><strong>在<code>app.module.ts</code>进行注册</strong></pre>
<pre class="typescript hljs"><code class="typescript"><span class="hljs-keyword">import { NgModule } <span class="hljs-keyword">from <span class="hljs-string">'@angular/core';
<span class="hljs-keyword">import { RouteReuseStrategy } <span class="hljs-keyword">from <span class="hljs-string">'@angular/router';
<span class="hljs-keyword">import { AppComponent } <span class="hljs-keyword">from <span class="hljs-string">'./app.component';
<span class="hljs-keyword">import { CustomReuseStrategy } <span class="hljs-keyword">from <span class="hljs-string">'./CustomReuseStrategy';
<span class="hljs-meta">@NgModule({
declarations: [
AppComponent
],
imports: [
<span class="hljs-comment">// your imports
],
providers: [
{ provide: RouteReuseStrategy, useClass: CustomReuseStrategy }
],
bootstrap:
})
<span class="hljs-keyword">export <span class="hljs-keyword">class AppModule { }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code><br><br><strong>第三步:</strong><br><span class="token keyword">const routes<span class="token operator">: Routes <span class="token operator">= <span class="token punctuation">[ <span class="token punctuation">.<span class="token punctuation">.<span class="token punctuation">.<span class="token punctuation">, <span class="token punctuation">{ path<span class="token operator">: <span class="token string">'class-list'<span class="token punctuation">, component<span class="token operator">: ClassListPage<span class="token punctuation">, data<span class="token operator">: <span class="token punctuation">{ reload<span class="token operator">: <span class="token boolean">true <span class="token punctuation">} <span class="token punctuation">} <span class="token punctuation">]<span class="token punctuation">;</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
<h3 id="item-2-2">删除路由快照</h3>
<pre class="typescript hljs"><code class="typescript"><span class="hljs-keyword">import { Component, OnInit } <span class="hljs-keyword">from <span class="hljs-string">'@angular/core';
<span class="hljs-keyword">import { CustomReuseStrategy } <span class="hljs-keyword">from <span class="hljs-string">'../r';
<span class="hljs-meta">@Component({
selector: <span class="hljs-string">'tabpage',
templateUrl: <span class="hljs-string">'./tabpage.component.html',
styleUrls: [<span class="hljs-string">'./tabpage.component.css'],
providers:
})
<span class="hljs-keyword">export <span class="hljs-keyword">class TodoComponent <span class="hljs-keyword">implements OnInit{
<span class="hljs-keyword">constructor(<span class="hljs-params">) {}
ngOnInit(): <span class="hljs-built_in">void {}
changeTab() {
<span class="hljs-comment">// 删除快照
<span class="hljs-keyword">this.deleteRouteSnapshot();
<span class="hljs-comment">// tab切换代码,路由跳转代码
<span class="hljs-comment">// ...
}
<span class="hljs-comment">/** 路由加载前可手动删除路由快照,切换路由则不会使用快照 */
deleteRouteSnapshot() {
CustomReuseStrategy.deleteRouteSnapshot(<span class="hljs-string">'/todolazy');
}
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<pre class="typescript hljs"><br><br></pre>
<h2 id="item-5">参考文章</h2>
<p>https://segmentfault.com/a/1190000015391814?utm_source=tag-newest</p>
<p>https://blog.csdn.net/lglspace/article/details/102983029</p><br><br>
来源:https://www.cnblogs.com/yinyueyu/p/13723325.html
頁:
[1]