简体   繁体   English

在Angular2路由中,URL已更改,但内容未更新

[英]In Angular2 routing, the URL changed but the content doesn't update

I am a beginner of Angular2. 我是Angular2的初学者。 When I am learning angular2 routing service from https://angular.io/tutorial/toh-pt5 . 当我从https://angular.io/tutorial/toh-pt5学习angular2路由服务时。 I want to have herocomponent always displayed so I in the app.component.html like this: 我想始终显示herocomponent,因此我在app.component.html中是这样的:

<h1>{{title}}</h1>
<nav>
  <a routerLink="/dashboard">Dashboard</a>
  <a routerLink="/heroes">Heroes</a>
  <a routerLink="/dummy">Dummy</a>
</nav>
<app-heroes></app-heroes>
<router-outlet></router-outlet>
<app-messages></app-messages>

And here is the app-routing.module.ts: 这是app-routing.module.ts:

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

import { DashboardComponent }   from './dashboard/dashboard.component';
import { HeroesComponent }      from './heroes/heroes.component';
import { HeroDetailComponent }  from './hero-detail/hero-detail.component';
import {DummyComponentComponent} from './dummy-component/dummy-component.component';
const routes: Routes = [
  { path: '', redirectTo: '/', pathMatch: 'full' },
  { path: 'dashboard', component: DashboardComponent },
  { path: 'detail/:id', component: HeroDetailComponent },
  { path: 'heroes', component: HeroesComponent },
  {path: 'dummy',component:DummyComponentComponent}
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}

Strangely, when I click a hero on /dashboard or /heroes page it can direct to the correct URL and show the correct hero detail. 奇怪的是,当我在/dashboard/heroes页面上单击英雄时,它可以定向到正确的URL并显示正确的英雄详细信息。

However, when I am on /detail/{{hero.id}} and click the hero on the hero component it can redirect the URL but the content in doesn't update. 但是,当我使用/detail/{{hero.id}}并单击hero组件上的hero时,它可以重定向URL,但其中的内容不会更新。 Can someone please help me with this? 有人可以帮我吗?

if below is not issue then it should be issue that you are just changing parameter url that is reason its not updating 如果下面没有问题,那应该是您正在更改参数url的问题,这是其未更新的原因

//put this code in your hero.component.ts file 
ngOnInit() {
this.route.params.subscribe((params) => {
   this.id = +params['id'];
   this.loadComponentAgain();
 });
}

above code monitor change in param value and update your component according to it. 上面的代码监视参数值的更改,并根据该参数更新组件。


There is issue because of this html 因为这个html而有问题

<app-heroes></app-heroes>
<router-outlet></router-outlet>

you are loading app-heroes component out side router-outlet , it should be loaded under router-outlet , so you should do this 要加载app-heroes COMPONENT OUT端router-outlet ,应该在加载router-outlet ,所以你应该这样做

<!-- <app-heroes></app-heroes> remove this-->
<router-outlet></router-outlet>

为了使用动态值更改路由,您需要像这样设置routerLink

[routerLink]="['/detail',hero.id]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM