Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic 4 + angular: routerLink only works first time

I'm having a weird bug while developing a basic app from scratch right now. I use Ionic 4 beta 19 and I've put a routerLink to another page, the route is set up in the base pages module like so:

RouterModule.forChild([
  { path: '', component: NewsPage },
  { path: ':id', component: DetailPage }
])

the routerLink attribute is set on a card and it works just fine when clicking on a card, but when I go back and press that same card or another one, the router just doesn't do anything at all. I don't get any errors and the URL in the browser is working perfectly. How can this be?

Edit: Also, DetailPage doesn't have a module so it's basically just a page.

Edit: Card code looks like this:

<ion-card *ngFor="let item of items;" [routerLink]="[item.id]">
  ...
</ion-card>

In the detail page, route params are subscribed and the :id param will be used for a GET request subscription to retrieve the data

like image 714
Yassine Zeriouh Avatar asked Dec 19 '18 21:12

Yassine Zeriouh


People also ask

How do I know if my routerLink is active?

routerLinkActive is simply an indicator of whether this link represents the active route. @jessepinho - Tried it - [routerLinkActive]="'active'" will only work if you also have [routerLink] in the a-tag. If you had (click)="navitageTo('/route')" instead of [routerLink], and in that function, you called this.

What does routerLink do in Angular?

RouterLink is a built-in Angular Directive that lets you link to specific routes in your app. In the SPA(single-page application), you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page.

Is routerLink relative?

Using RouterLink Directive RouterLink directive has support of ActivatedRoute automatically for relative navigation. We can use relative path and absolute path both with RouterLink directive.

What is difference between routerLink and routerLink?

What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.


4 Answers

This is already fixed, just run npm i @ionic/angular to update to the fixing version 4.2.0 or superior.

This was also affecting router.navigate(['url', params]) functionality

like image 195
Lovenkrands Avatar answered Oct 24 '22 08:10

Lovenkrands


If it is possible, a workaround is to use absolute path instead of relative path as said in this github answer :

I will leave this issue open, and we will try to fix, but it's far from trivial and using absolute paths is a good workaround!

We are aware of this issue :)

So you will have :

<ion-item routerLink="/home" />

instead of :

<ion-item routerLink="home" />
like image 43
Stephane L Avatar answered Oct 24 '22 07:10

Stephane L


Solved it by using navigateByUrl instead like this:

open(id: number) {
  this.router.navigateByUrl(this.router.url + '/' + id);
}

and for the card:

<ion-card *ngFor="let item of items;" (click)="open(item.id)">
    ...
</ion-card>

Still don't know why this is happening but this works as a workaround for now.

like image 4
Yassine Zeriouh Avatar answered Oct 24 '22 09:10

Yassine Zeriouh


This is a known bug. You can find the GitHub issue here: https://github.com/ionic-team/ionic/issues/16534.

As @mario above mentions, absolute links will work with routerLink. See comment here: https://github.com/ionic-team/ionic/issues/16534#issuecomment-444610330

like image 2
Nicholas Pretorius Avatar answered Oct 24 '22 08:10

Nicholas Pretorius