docs: fix routing code to use navigate
(#35176)
Previously, the example in the `router` guide was not preserving the query params after logging in. This commit fixes this by using `navigate` instead of using `navigateByUrl`, which ignores any properties in the `NavigationExtras` that would change the provided URL. Fixes 34917 PR Close #35176
This commit is contained in:

committed by
Miško Hevery

parent
ea991f7edf
commit
a7d5c55926
@ -1,6 +1,6 @@
|
||||
// #docregion
|
||||
import { Component } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { Component } from '@angular/core';
|
||||
import { Router } from '@angular/router';
|
||||
import { AuthService } from '../auth.service';
|
||||
|
||||
@Component({
|
||||
@ -25,12 +25,12 @@ export class LoginComponent {
|
||||
this.authService.login().subscribe(() => {
|
||||
this.setMessage();
|
||||
if (this.authService.isLoggedIn) {
|
||||
// Get the redirect URL from our auth service
|
||||
// If no redirect has been set, use the default
|
||||
let redirect = this.authService.redirectUrl ? this.router.parseUrl(this.authService.redirectUrl) : '/admin';
|
||||
// Usually you would use the redirect URL from the auth service.
|
||||
// However to keep the example simple, we will always redirect to `/admin`.
|
||||
const redirectUrl = '/admin';
|
||||
|
||||
// Redirect the user
|
||||
this.router.navigateByUrl(redirect);
|
||||
this.router.navigate([redirectUrl]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
// #docregion
|
||||
import { Component } from '@angular/core';
|
||||
import { Router,
|
||||
NavigationExtras } from '@angular/router';
|
||||
import { AuthService } from '../auth.service';
|
||||
import { Component } from '@angular/core';
|
||||
import { NavigationExtras, Router } from '@angular/router';
|
||||
import { AuthService } from '../auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
@ -26,9 +25,9 @@ export class LoginComponent {
|
||||
this.authService.login().subscribe(() => {
|
||||
this.setMessage();
|
||||
if (this.authService.isLoggedIn) {
|
||||
// Get the redirect URL from our auth service
|
||||
// If no redirect has been set, use the default
|
||||
let redirect = this.authService.redirectUrl ? this.router.parseUrl(this.authService.redirectUrl) : '/admin';
|
||||
// Usually you would use the redirect URL from the auth service.
|
||||
// However to keep the example simple, we will always redirect to `/admin`.
|
||||
const redirectUrl = '/admin';
|
||||
|
||||
// #docregion preserve
|
||||
// Set our navigation extras object
|
||||
@ -39,7 +38,7 @@ export class LoginComponent {
|
||||
};
|
||||
|
||||
// Redirect the user
|
||||
this.router.navigateByUrl(redirect, navigationExtras);
|
||||
this.router.navigate([redirectUrl], navigationExtras);
|
||||
// #enddocregion preserve
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user