【问题标题】:change data on route event in Angular在 Angular 中更改路由事件的数据
【发布时间】:2018-05-25 22:46:24
【问题描述】:

如何在 Angular 中的路由事件中相对于其他组件更改一个组件中的数据?

例如如果我有三个组件:"nav.component""about.component""service.component"

所以当我在我的应用程序中的aboutservice 页面之间切换时,我想在"nav.component" 中显示不同的文本。

我的"app.router.ts" 文件:

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { AboutComponent } from './about/about.component';
import { ServiceComponent } from './service/service.component';

export const router: Routes = [
    { path: '', redirectTo: 'about', pathMatch: 'full' },
    { path: 'about', component: AboutComponent },
    { path: 'service', component: ServiceComponent }
];

export const routes: ModuleWithProviders = RouterModule.forRoot(router);

在这些页面之间切换时,我不想在导航栏中只显示页面名称文本,这将是每个组件的自定义文本。 此外,由于可维护性和可扩展性,我想将此数据/文本直接存储在 "about.component.ts""service.component.ts" 中,但不存储在 "app.router.ts" 中。

有可能吗?

U.P.D.

这是我的"app.component.html" 文件:

<div class="container">

  <!-- Nav Bar (text changes here) -->
  <app-nav></app-nav>

  <!-- Pages (components which are included in app.router.ts) -->
  <router-outlet></router-outlet>

</div>

例如这是"about.component.ts" 文件:

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

@Component({
  selector: 'app-about',
  templateUrl: './about.component.html',
  styleUrls: ['./about.component.scss']
})
export class AboutComponent implements OnInit {
  const text_for_nav_bar = "This is my new About page."; // <-- text that should be displayed in nav component for this page on router event.

  constructor() { }

  ngOnInit() {
  }

}

【问题讨论】:

  • 您的要求不明确。什么是导航组件?您的应用程序结构如何?自定义文本是什么意思?你想在哪里显示它?回答您的问题需要所有这些信息。
  • 你可以通过路由更改事件做到这一点:stackoverflow.com/questions/33520043/…
  • @VinodBhavnani,我已经更新了我的帖子,希望这会有所帮助。我想为每个组件存储自定义文本字符串,此字符串应显示在 nav 组件中,具体取决于路由器。

标签: javascript angular components


【解决方案1】:

你应该使用路由器数据

export const router: Routes = [
    { path: '', redirectTo: 'about', pathMatch: 'full' },
    { path: 'about', component: AboutComponent, data: {navigationText: 'Some text'} },
    { path: 'service', component: ServiceComponent, data: {navigationText: 'Some other text'} }
];

在 app.component.html 中

<div class="container">

    <app-nav text="outlet.activatedRouteData.navigationText"></app-nav>

    <router-outlet #outlet="outlet"></router-outlet>

</div>

当然你需要在nav.component.ts中添加一个“@Input text: string”属性

【讨论】:

    【解决方案2】:

    使用以下代码,您将能够订阅路由器更改事件。您需要在导航栏上添加此代码。

    导入路由器和导航开始

    import { Router, ActivatedRoute, NavigationStart } from '@angular/router';
    import "rxjs/add/operator/filter";
    import "rxjs/add/operator/pairwise";
    

    在 constrictor 中添加以下代码。

    this.router.events
      .filter(event => event instanceof NavigationStart)
      .pairwise()
      .subscribe((value: [NavigationStart, NavigationStart]) => {
         let nextUrl = value[1].url;
         if (nextUrl == '/about') {
             // your code here for next url
         }
      },
        (err) => {
    
        },
        () => { });
    }
    
    });
    

    【讨论】:

    • 对不起,出现错误:Property 'filter' does not exist on type 'Observable&lt;Event&gt;'
    • 谢谢!现在它编译得很好,但是当我试图将{{ nextUrl }} 放入我的模板时,nextUrl 不包含字符串:(
    • 在这种情况下使 nextUrl 成为全局变量。
    • 谢谢,它有效!但是如何在页面组件中存储文本数据,并在导航栏组件的路由事件中获取它们呢?
    • 使其成为公共静态或将它们存储到本地存储和访问
    【解决方案3】:

    一种方法是使用*ngIf(或[hidden],如果您想一次将所有内容加载到DOM)。为了捕捉当前路由,注入 Router 模块:

    class NavComponent {
        constructor(private router: Router){
    
        }
    }
    

    在 nav.component.html 中:

    <div *ngIf="router.url === '/some/route'">
     text for this route
    </div>
    <div *ngIf="router.url === '/other/route'">
     text for other route
    </div>
    

    在 component.ts 中做同样的事情,可能是:

    nav.component.html:

    <h1>{{yourText}}</h1>
    

    component.ts:

    ngOnInit() {
      if(this.router.url == '/some/route') {
        yourText = 'Text'
      } elseif(this.router.url == '/other/route') {
        yourText = 'Other text'
      }
    }
    

    【讨论】:

    • 现在我认为可能需要触发更改检测并使用其他循环挂钩。如果它不起作用,请告诉我。有一段时间我用 Angular 做了一些事情
    • 我刚刚检查了您上面的示例,不幸的是它不起作用。 this.router.url 不返回路径,只返回一个斜线。
    • 所以它可能返回根路径。但它在导航时永远不会改变,因为 navComponent 只初始化了一次。正如@Shabbir Dhangot 所写,订阅路由器事件是正确的方式,只是我不能保证他的逻辑(我有点忘记了 Angular)
    • 总而言之,*ngIfrouter.url 效果很好,但在这种情况下有点不合适。
    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多