Angular 2 - 浏览网页而无需重新加载这些页面常用的组件 [英] Angular 2 — navigate through web pages without reloading a component that is common for those pages

查看:125
本文介绍了Angular 2 - 浏览网页而无需重新加载这些页面常用的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里您可以找到一个示例应用程序: http://ivan-khludov.com/

Here you can find an example app: http://ivan-khludov.com/

这是我的根组件:

import { Component } from '@angular/core';

@Component({
  selector: 'root',
  template: `
  <h1>My Dummy Angular App</h1>
  <router-outlet></router-outlet>
  <nav>
    <a routerLink="/section-1/1" routerLinkActive="active">Section 1 - Page 1</a>
    <span>||</span>
    <a routerLink="/section-1/2" routerLinkActive="active">Section 1 - Page 2</a>
    <span>||</span>
    <a routerLink="/section-2/1" routerLinkActive="active">Section 2 - Page 1</a>
    <span>||</span>
    <a routerLink="/section-2/2" routerLinkActive="active">Section 2 - Page 2</a>
  </nav>
  `
})

export class AppWrapper {

}

第1部分第1页的组件:

A component for section 1, page 1:

import { Component } from '@angular/core';

@Component({
  selector: 'secapagea',
  template: `
  <h2>Section 1 - Page 1</h2>
  <countera></countera>
  `
})

export class Section1Page1 {

}

第1部分第2页几乎相同:

Almost the same thing for section 1, page 2:

import { Component } from '@angular/core';

@Component({
  selector: 'secapageb',
  template: `
  <h2>Section 1 - Page 2</h2>
  <countera></countera>
  `
})

export class Section1Page2 {

}

计数器组件:

import { Component } from '@angular/core';

@Component({
  selector: 'countera',
  template: `
  <div>Seconds elapsed: {{this.count}}</div>
  `
})

export class Section1Counter {

    count: number;

    constructor() {

        this.count = 0;

        setInterval(() => {

            this.count ++;

        }, 1000);

    }

}

放案我打开了该部分的第一页。有没有办法导航到同一部分的第二页而无需重新加载计数器?我想找到这种导航问题的一般解决方案 - 它可能不是计数器组件,而是侧栏导航或节标题或其他。

Put the case that I opened the first page of the section. Is there a way to navigate to the second page of the same section without reloading the counter? I want to find a general solution for such navigation issues — it could be not a counter component but sidebar navigation or section heading or something else.

推荐答案

是的,绝对有可能编写一个可以处理多个路由而无需每次都重构Component的Component。

Yeah it is absolutely possible to write a Component which can handle multiple Routes without reconstructing the Component each time.

如果你创建一个组件来处理所有页面按以下路线定义:

If you create a component which handles all pages together as defined with the following route:

const routes: Routes = [
  { path: 'section-1/:page', component: Section1PageX }
];

您可以订阅路线参数page并处理组件中的页面更改。这可以防止Angular2每次重建页面组件。

You can subscribe on the route parameter "page" and handle the page change within your Component. This prevents Angular2 from reconstructing the page Component each time.

@Component({
  selector: 'secapagea',
  template: `
  <h2>Section 1 - Page {{page}}</h2>
  <countera></countera>
  `
})
export Section1PageX {
  private page: string;

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
       this.page = params['page'];
       //handle the page change
    });  
  }

  ngOnDestroy() {
    //unsubscribe when you leave the section
    this.sub.unsubscribe();
  }
}

所以你的Section1Counter组件只有在你离开时才会被销毁整个部分。

So your Section1Counter Component will only be destroyed if you leave the entire section.

您还可以在我们的博文中了解更多相关信息 Angular 2 by Example

You can also read more about this in our Blog Post Angular 2 by Example

这篇关于Angular 2 - 浏览网页而无需重新加载这些页面常用的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆