zoukankan      html  css  js  c++  java
  • [Angular2 Router] Exiting an Angular 2 Route

    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()
      }
    
    }

    Github

  • 相关阅读:
    关闭编辑easyui datagrid table
    sql 保留两位小数+四舍五入
    easyui DataGrid 工具类之 util js
    easyui DataGrid 工具类之 后台生成列
    easyui DataGrid 工具类之 WorkbookUtil class
    easyui DataGrid 工具类之 TableUtil class
    easyui DataGrid 工具类之 Utils class
    easyui DataGrid 工具类之 列属性class
    oracle 卸载
    “云时代架构”经典文章阅读感想七
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5918470.html
Copyright © 2011-2022 走看看