[Angular2 Router] Exiting an Angular 2 Route - How To Prevent Memory Leaks
In this tutorial we are going to learn how we can accidentally creating memory leaks in our application while using the Angular 2 router. We are going to learn how we can prove that the memory leak is happening, we are going to learn what is causing it and how we can fix it.
This is an issue that might become visible in very large applications which load a lot of data from the backend, and might cause errors that can be very hard to troubleshoot.
import {Component, OnInit, OnDestroy} from '@angular/core';
import {ActivatedRoute, RouterStateSnapshot, Router} from "@angular/router";
import {StarWarsService} from "../heros.service";
import {Observable, Subscription} from "rxjs";
@Component({
selector: 'app-hero',
templateUrl: 'hero.component.html',
styleUrls: ['hero.component.css']
})
export class HeroComponent implements OnInit, OnDestroy {
hero: Observable<any>;
description: string;
querySub: Subscription;
constructor(private route: ActivatedRoute,
private router: Router,
private starwarService: StarWarsService) {
}
ngOnInit() {
/* this.hero = this.router.params
.map((p:any) => p.id)
.switchMap( id => this.starwarService.getPersonDetail(id));
*/
// since herocomponent get init everytime, it would be better to use snapshot for proferemence
const heroId = this.route.snapshot.params['id'];
this.hero = this.starwarService.getPersonDetail(heroId);
this.querySub = this.route.queryParams.subscribe(
param => this.description = param['description']
);
console.log("observers", this.route.queryParams['observers'].length)
}
ngOnDestroy(){
this.querySub.unsubscribe()
}
}
[Angular2 Router] Exiting an Angular 2 Route - How To Prevent Memory Leaks的更多相关文章
- [Angular2 Router] Lazy Load Angular 2 Modules with the Router
Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start ...
- [Angular2 Router] Understand the Angular 2 Base href Requirement
The <base href=”/”/> you define will determine how all other assets you plan on loading treat ...
- [Angular2 Router] Resolving route data in Angular 2
From Article: RESOLVING ROUTE DATA IN ANGULAR 2 Github If you know Anuglar UI router, you must know ...
- [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable
In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parame ...
- [Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation
In this tutorial we are going to learn how to navigate programmatically (or imperatively) by using t ...
- [Angular2 Router] Auxiliary Routes bit by bit
Article Github Auxiliary Routes is is little bit hard to understand. Here is demo, link You can see ...
- 基于angular的route实现单页面cnodejs
Angular ui-router 前言 之前不太理解前端怎么实现路由功能,以前知道有一种方式使用html5的pushState可以操作url才实现路由的功能,在实践项目中也用过一次,后来这种操作叫成 ...
- angular2 Router类中的路由跳转navigate
navigate是Router类的一个方法,主要用来路由跳转. 函数定义 navigate(commands: any[], extras?: NavigationExtras) : Promise` ...
- [Angular2 Router] Load Data Based on Angular 2 Route Params
You can load resource based on the url using the a combination of ActivatedRouteand Angular 2’s Http ...
随机推荐
- Unreal Engine 虚幻引擎宣布对开发者免费
虚幻引擎4现在可供每个人免费使用,而且所有未来的更新都将免费!您可以下载引擎并将其用于游戏开发的各个方面,包括教育.建筑以及可视化,甚至虚拟现 实.电影和动画. 当您发布游戏或应用时,在您的每个游戏在 ...
- (转载)OC学习篇之---类目的概念和使用
上一篇文章介绍了OC中的@class关键字的使用,这一篇我们介绍一下,OC中的一个特有的亮点:类目 首先我们来看一下场景,如果我们现在想对一个类进行功能的扩充,我们该怎么做? 对于面向对象编程的话,首 ...
- python堡垒机
堡垒机 windows下安装python3的paramiko模块后一些报错总结: error: Unable to find vcvarsall.bat [官网对此问题的描述] : https://d ...
- CentOS下挂在NTFS分区
本文参考自http://www.cnblogs.com/gbyukg/archive/2011/11/02/2232343.html centos下想访问NTFS分区,现在普遍的方法是利用ntfs-3 ...
- Python 批量创建同文件名的特定后缀文件
看了很多批量创建文件和文件批量格式转换的code,感觉杀鸡焉用牛刀,自己写了几行轻量级的拿来给大家参考: 在out_dir目录下批量创建与in_dir目录下同文件名但后缀不同的文件. in_dir = ...
- CameraComponent Quality
CameraComponent1.Quality := TVideoCaptureQuality.HighQuality; procedure TCameraComponentForm.Set720p ...
- 【转】eclipse.ini内存设置
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 这里有几个问题:1. 各个参数的含义什么?2. 为什么有的机器我将-Xm ...
- Java反射机制(Class类的使用)
1:通过无参构造实例化对象 package cn.itcast; /* * 通过无参构造实例化对象 * 通过Class类本身实例化对象,使用newInstance方法 * 需要注意的是:实例化类中存在 ...
- CSS元素水平居中和垂直居中的方法大全
水平居方法: 1.最熟悉的是给元素定义一个宽度,然后使用margin: body{ width:960px; margin:0 auto;}这个是当我们的定义元素的宽度时显现的,如果我们不能定义宽度时 ...
- POJ 1947 Rebuilding Roads (树dp + 背包思想)
题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...