Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #docregion
import { Component, HostBinding } from '@angular/core';
import { Router } from '@angular/router';
import {Component} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
selector: 'app-compose-message',
Expand All @@ -12,7 +12,7 @@ export class ComposeMessageComponent {
message: string;
sending = false;

constructor(private router: Router) {}
constructor(private router: Router, private route: ActivatedRoute) {}

send() {
this.sending = true;
Expand All @@ -32,7 +32,7 @@ export class ComposeMessageComponent {
closePopup() {
// Providing a `null` value to the named outlet
// clears the contents of the named outlet
this.router.navigate([{ outlets: { popup: null }}]);
this.router.navigate([{outlets: {popup: null}}], {relativeTo: this.route.parent});
}
// #enddocregion closePopup
}
23 changes: 23 additions & 0 deletions aio/content/guide/router-tutorial-toh.md
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,29 @@ This time, the value of `'popup'` is `null`.
That's not a route, but it is a legitimate value.
Setting the popup `RouterOutlet` to `null` clears the outlet and removes the secondary popup route from the current URL.

<div class="alert is-critical">

**Note:** All commands in the array passed to `Router.navigate()` target a _specific segment_ in the `UrlTree`.
Comment thread
atscott marked this conversation as resolved.
We specify the parent of the `ActivatedRoute` as the `relativeTo` option because we want to remove `'popup'` from the segment which holds its reference.
Comment thread
atscott marked this conversation as resolved.
It's important to always be aware of which segments the commands will be applied to.
Comment thread
atscott marked this conversation as resolved.

</div>

<div class="alert is-helpful">

When `relativeTo` is not provided to the `Router.navigate()` method, the commands are processed starting at the root.
Comment thread
atscott marked this conversation as resolved.
We could omit the `relativeTo` option in this particular example because the `'popup'` outlet appears at the root level of the configuration.
Comment thread
atscott marked this conversation as resolved.

</div>

<div class="alert is-helpful">

If you want to close an outlet which appears at any segment depth, you could accomplish
this by creating a `UrlTree` from the current URL, recursively clearing segment `children` matching the outlet name, and finally
calling `Router.navigateByUrl()` with the `root` segment of the current `UrlTree`.
Comment thread
atscott marked this conversation as resolved.

</div>

{@a guards}

{@a milestone-5-route-guards}
Expand Down