Skip to content

Commit ca00dfb

Browse files
committed
build: links to headers not working (#31436)
Fixes that a previous commit stopped setting the `example` on the `HeaderLink` which means that its link isn't constructed correctly. (cherry picked from commit ea7148c)
1 parent 7b87677 commit ca00dfb

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

docs/src/app/shared/doc-viewer/doc-viewer.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,17 @@ export class DocViewer implements OnDestroy {
187187
const examplePortal = new ComponentPortal(componentClass, this._viewContainerRef);
188188
const exampleViewer = portalHost.attach(examplePortal);
189189
const exampleViewerComponent = exampleViewer.instance;
190-
if (example !== null && componentClass === ExampleViewer) {
191-
DocViewer._initExampleViewer(
192-
exampleViewerComponent as ExampleViewer,
193-
example,
194-
file,
195-
region,
196-
);
190+
if (example !== null) {
191+
if (componentClass === ExampleViewer) {
192+
DocViewer._initExampleViewer(
193+
exampleViewerComponent as ExampleViewer,
194+
example,
195+
file,
196+
region,
197+
);
198+
} else {
199+
(exampleViewerComponent as HeaderLink).example.set(example);
200+
}
197201
}
198202
this._portalHosts.push(portalHost);
199203
});

docs/src/app/shared/doc-viewer/header-link.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {Component, inject} from '@angular/core';
9+
import {Component, computed, inject, signal} from '@angular/core';
1010
import {Router} from '@angular/router';
1111
import {MatIcon} from '@angular/material/icon';
1212

@@ -18,7 +18,7 @@ import {MatIcon} from '@angular/material/icon';
1818
selector: 'header-link',
1919
template: `
2020
<a aria-label="Link to this heading" class="docs-markdown-a"
21-
[attr.aria-describedby]="example" [href]="_getFragmentUrl()">
21+
[attr.aria-describedby]="example()" [href]="_fragmentUrl()">
2222
<mat-icon>link</mat-icon>
2323
</a>
2424
`,
@@ -29,18 +29,12 @@ export class HeaderLink {
2929
* Id of the anchor element. Note that is uses "example" because we instantiate the
3030
* header link components through the ComponentPortal.
3131
*/
32-
example: string = '';
32+
readonly example = signal('');
3333

3434
/** Base URL that is used to build an absolute fragment URL. */
35-
private _baseUrl: string;
35+
private _baseUrl = inject(Router).url.split('#')[0];
3636

37-
constructor() {
38-
const router = inject(Router);
39-
40-
this._baseUrl = router.url.split('#')[0];
41-
}
42-
43-
_getFragmentUrl(): string {
44-
return `${this._baseUrl}#${this.example}`;
45-
}
37+
protected readonly _fragmentUrl = computed(() => {
38+
return `${this._baseUrl}#${this.example()}`;
39+
});
4640
}

0 commit comments

Comments
 (0)