From 2064508876231e82c1f59b0d6de013c826449f2d Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 12 Mar 2019 19:49:39 +0000 Subject: [PATCH] docs: fix redirect with secondary outlet in router guide example (#29267) The URL wasn't be parsed into a UrlTree, so redirecting with a secondary route went to a 404 Closes #28805 PR Close #29267 --- .../examples/router/e2e/src/app.e2e-spec.ts | 26 +++++++++++++++++++ .../src/app/auth/login/login.component.1.ts | 4 +-- .../src/app/auth/login/login.component.ts | 4 +-- aio/content/guide/router.md | 4 +-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/aio/content/examples/router/e2e/src/app.e2e-spec.ts b/aio/content/examples/router/e2e/src/app.e2e-spec.ts index 41d3a49c9a..9d0d9ed8bf 100644 --- a/aio/content/examples/router/e2e/src/app.e2e-spec.ts +++ b/aio/content/examples/router/e2e/src/app.e2e-spec.ts @@ -31,6 +31,7 @@ describe('Router', () => { heroDetailTitle: heroDetail.element(by.xpath('*[2]')), adminHref: hrefEles.get(2), + adminPage: element(by.css('app-root > div > app-admin')), adminPreloadList: element.all(by.css('app-root > div > app-admin > app-admin-dashboard > ul > li')), loginHref: hrefEles.get(3), @@ -138,6 +139,31 @@ describe('Router', () => { expect(page.secondaryOutlet.count()).toBe(1, 'secondary outlet'); }); + it('should redirect with secondary route', async () => { + const page = getPageStruct(); + + // go to login page and login + await browser.get(''); + await page.loginHref.click(); + await page.loginButton.click(); + + // open secondary outlet + await page.contactHref.click(); + + // go to login page and logout + await page.loginHref.click(); + await page.loginButton.click(); + + // attempt to go to admin page, redirects to login with secondary outlet open + await page.adminHref.click(); + + // login, get redirected back to admin with outlet still open + await page.loginButton.click(); + + expect(await page.adminPage.isDisplayed()).toBeTruthy(); + expect(page.secondaryOutlet.count()).toBeTruthy(); + }); + async function crisisCenterEdit(index: number, save: boolean) { const page = getPageStruct(); await page.crisisHref.click(); diff --git a/aio/content/examples/router/src/app/auth/login/login.component.1.ts b/aio/content/examples/router/src/app/auth/login/login.component.1.ts index 2986631aa1..0879347c70 100644 --- a/aio/content/examples/router/src/app/auth/login/login.component.1.ts +++ b/aio/content/examples/router/src/app/auth/login/login.component.1.ts @@ -27,10 +27,10 @@ export class LoginComponent { 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.authService.redirectUrl : '/crisis-center/admin'; + let redirect = this.authService.redirectUrl ? this.router.parseUrl(this.authService.redirectUrl) : '/admin'; // Redirect the user - this.router.navigate([redirect]); + this.router.navigateByUrl(redirect); } }); } diff --git a/aio/content/examples/router/src/app/auth/login/login.component.ts b/aio/content/examples/router/src/app/auth/login/login.component.ts index 5ebfd58b21..ca3eb70a34 100644 --- a/aio/content/examples/router/src/app/auth/login/login.component.ts +++ b/aio/content/examples/router/src/app/auth/login/login.component.ts @@ -28,7 +28,7 @@ export class LoginComponent { 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.authService.redirectUrl : '/admin'; + let redirect = this.authService.redirectUrl ? this.router.parseUrl(this.authService.redirectUrl) : '/admin'; // #docregion preserve // Set our navigation extras object @@ -39,7 +39,7 @@ export class LoginComponent { }; // Redirect the user - this.router.navigate([redirect], navigationExtras); + this.router.navigateByUrl(redirect, navigationExtras); // #enddocregion preserve } }); diff --git a/aio/content/guide/router.md b/aio/content/guide/router.md index 1c9ff15734..066318d1f6 100644 --- a/aio/content/guide/router.md +++ b/aio/content/guide/router.md @@ -3849,7 +3849,7 @@ Update the `AuthGuard` to provide a `session_id` query that will remain after na Add an `anchor` element so you can jump to a certain point on the page. -Add the `NavigationExtras` object to the `router.navigate` method that navigates you to the `/login` route. +Add the `NavigationExtras` object to the `router.navigate()` method that navigates you to the `/login` route. @@ -3860,7 +3860,7 @@ Add the `NavigationExtras` object to the `router.navigate` method that navigates You can also preserve query parameters and fragments across navigations without having to provide them again when navigating. In the `LoginComponent`, you'll add an *object* as the -second argument in the `router.navigate` function +second argument in the `router.navigateUrl()` function and provide the `queryParamsHandling` and `preserveFragment` to pass along the current query parameters and fragment to the next route.