简体   繁体   English

Angular 5 RouterLink不能使用相同的路径但不同的参数

[英]Angular 5 RouterLink does not work with same path but different parameters

I have a anchor element with a RouterLink element that takes parameters. 我有一个带元素的锚元素,它带有参数。 The parameters are necessary in order to navigate to the correct page. 这些参数是导航到正确页面所必需的。 Now the router link works when I navigate the page from a different path. 现在,当我从不同的路径导航页面时,路由器链接正常工作。 For instance my router module looks like this: 例如,我的路由器模块如下所示:

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

import { MoviesComponent } from './movies/movies.component';
import { MovieReviewComponent } from './movie-review/movie-review.component';
import { LoginComponent } from './login/login.component';
import { AdminComponent } from './admin/admin.component';
const routes: Routes = [
  { path: '', component: MoviesComponent },
  { path: 'movies', component: MoviesComponent },
  { path: 'movies/:movieTitle/:year', component: MovieReviewComponent },
  { path: 'login', component: LoginComponent },
  { path: 'admin', component: AdminComponent }
];

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

And I have a navigation component located outside the router-outlet like this: 我在路由器插座外面有一个导航组件,如下所示:

<app-navigation></app-navigation>
<app-messages></app-messages>
<div id="main-layout" class="row p-3">
  <section class="col-12 col-xm-12 col-md-3">hi</section>
  <section class="col-12 col-xm-12 col-md-6">
    <router-outlet></router-outlet>
  </section>
  <section class="col-12 col-xm-12 col-md-3">lo</section>
</div>

My navigation has a search bar that uses RouterLink to route to path 'movies/:movieTitle/:year' and works as long as I'm coming from a path that isn't like 'movies/:movieTitle/:year' . 我的导航有一个搜索栏,它使用RouterLink路由到路径'movies/:movieTitle/:year'并且只要我来自一条不像'movies/:movieTitle/:year'的路径就行。 If the paths are similar angular will not rerender even if the movieTitle and year arguments are different. 如果路径类似,即使movieTitle和year参数不同,angular也不会重新渲染。

For example if I'm at home '/' or 'movies' I can use the navbar to successfully go to 'movies/The%20Dark%20Knight/2008' . 例如,如果我在家里'/''movies'我可以使用导航栏成功转到'movies/The%20Dark%20Knight/2008' But if I'm on 'movies/The%20Dark%20Knight/2008' I cannot use the navbar to go to '/movies/Taken/2008' . 但如果我在'movies/The%20Dark%20Knight/2008'我不能使用导航栏去'/movies/Taken/2008' I believe this has to do with Angular trying to decrease rerendering for similar paths, but how do I get Angular to change views when the same path-type has different argument? 我相信这与Angular试图减少类似路径的重新渲染有关,但是当相同的路径类型具有不同的参数时,如何让Angular改变视图?

Extra Info 额外信息

  • Even when the route fails to render a new view the path url changes in the browser and it is the same url for when the page change is successfull 即使路线无法呈现新视图,路径网址也会在浏览器中更改,并且与页面更改成功时的网址相同
  • I have angular navigation events in my navigation component. 我的导航组件中有角度导航事件。 I can confirm that navigation starts and that there is no navigation error. 我可以确认导航开始并且没有导航错误。 Still have to test if it cancels or not. 还是要测试它是否取消。

Answer 回答

As the answer says I needed to subscribe to the params. 正如答案所说,我需要订阅params。 Previously when I was acquiring the params I was using a snapshot: 以前当我获得params时,我正在使用快照:

movieTitle = this.route.snapshot.paramMap.get('movieTitle');

Now I get the params directly and subscribe to them: 现在我直接得到params并订阅它们:

this.route.params.subscribe(params => {
      this.movieService.getMovie(params['movieTitle'], params['year'])
        .subscribe(movie => {
          this.movie = movie
        })
    })

Routes with empty path and no children should have pathMatch: 'full' 路径为空且没有孩子的路线应该有pathMatch:'full'

{ path: '', component: MoviesComponent, pathMatch: 'full' },

also the order of routes matters. 路线的顺序也很重要。 More specific routes should come first: 首先应该有更具体的路线:

{ path: 'movies/:movieTitle/:year', component: MovieReviewComponent },
{ path: 'movies', component: MoviesComponent },

If only route parameters change, but the route stays the same, Angular reuses the component. 如果只有路由参数发生变化,但路由保持不变,则Angular会重用该组件。 You need to subscribe to params to get notified about changes. 您需要订阅params以获得有关更改的通知。 The URL is still updated. 该URL仍在更新中。

As @Günter Zöchbauer said in his answer, you need to subscribe to params to get notified about the changes. 正如@Günter Zöchbauer在回答中所说,您需要订阅params以获得有关更改的通知。 Here is how you can do that... 这是你如何做到这一点......

Suppose you are in OrderDetailComponent and you want to show the details of a specific order, the order with the id which exists in the URL like /orders/1 and properly shows the right order if the id changed like /orders/2 , /orders/3 , ... here is the code which performs that... 假设您在OrderDetailComponent并且您希望显示特定订单的详细信息,具有ID的/orders/1/orders/1并正确显示正确的订单,如果ID更改为/orders/2/orders/3 ,...这是执行该操作的代码......

// order-detail.component.ts file

export class OrderDetailComponent implements OnInit {

    constructor(private activatedRoute: ActivatedRoute) { }

    ngOnInit() {
        // Subscribing to the route params is important as it fixes an issue as
        // Angular RouterLink may not work with same path but different params
        this.activatedRoute.params.subscribe(params => {
            console.log('URL Params', params);
            this.getOrder(+params.id);
        });
    }

    getOrder(id: number) {
        // Your code to get the order with the provided id...
    }
}

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

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