package.esm2022.src.page_title_strategy.mjs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of router Show documentation
Show all versions of router Show documentation
Angular - the routing library
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { inject, Injectable } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { PRIMARY_OUTLET, RouteTitleKey } from './shared';
import * as i0 from "@angular/core";
import * as i1 from "@angular/platform-browser";
/**
* Provides a strategy for setting the page title after a router navigation.
*
* The built-in implementation traverses the router state snapshot and finds the deepest primary
* outlet with `title` property. Given the `Routes` below, navigating to
* `/base/child(popup:aux)` would result in the document title being set to "child".
* ```
* [
* {path: 'base', title: 'base', children: [
* {path: 'child', title: 'child'},
* ],
* {path: 'aux', outlet: 'popup', title: 'popupTitle'}
* ]
* ```
*
* This class can be used as a base class for custom title strategies. That is, you can create your
* own class that extends the `TitleStrategy`. Note that in the above example, the `title`
* from the named outlet is never used. However, a custom strategy might be implemented to
* incorporate titles in named outlets.
*
* @publicApi
* @see [Page title guide](guide/routing/common-router-tasks#setting-the-page-title)
*/
export class TitleStrategy {
/**
* @returns The `title` of the deepest primary route.
*/
buildTitle(snapshot) {
let pageTitle;
let route = snapshot.root;
while (route !== undefined) {
pageTitle = this.getResolvedTitleForRoute(route) ?? pageTitle;
route = route.children.find((child) => child.outlet === PRIMARY_OUTLET);
}
return pageTitle;
}
/**
* Given an `ActivatedRouteSnapshot`, returns the final value of the
* `Route.title` property, which can either be a static string or a resolved value.
*/
getResolvedTitleForRoute(snapshot) {
return snapshot.data[RouteTitleKey];
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.6", ngImport: i0, type: TitleStrategy, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.6", ngImport: i0, type: TitleStrategy, providedIn: 'root', useFactory: () => inject(DefaultTitleStrategy) }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.6", ngImport: i0, type: TitleStrategy, decorators: [{
type: Injectable,
args: [{ providedIn: 'root', useFactory: () => inject(DefaultTitleStrategy) }]
}] });
/**
* The default `TitleStrategy` used by the router that updates the title using the `Title` service.
*/
export class DefaultTitleStrategy extends TitleStrategy {
constructor(title) {
super();
this.title = title;
}
/**
* Sets the title of the browser to the given value.
*
* @param title The `pageTitle` from the deepest primary route.
*/
updateTitle(snapshot) {
const title = this.buildTitle(snapshot);
if (title !== undefined) {
this.title.setTitle(title);
}
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.6", ngImport: i0, type: DefaultTitleStrategy, deps: [{ token: i1.Title }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "18.2.6", ngImport: i0, type: DefaultTitleStrategy, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.6", ngImport: i0, type: DefaultTitleStrategy, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], ctorParameters: () => [{ type: i1.Title }] });
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGFnZV90aXRsZV9zdHJhdGVneS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uL3BhY2thZ2VzL3JvdXRlci9zcmMvcGFnZV90aXRsZV9zdHJhdGVneS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxPQUFPLEVBQUMsTUFBTSxFQUFFLFVBQVUsRUFBQyxNQUFNLGVBQWUsQ0FBQztBQUNqRCxPQUFPLEVBQUMsS0FBSyxFQUFDLE1BQU0sMkJBQTJCLENBQUM7QUF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