Skip to content

Commit f58ad86

Browse files
atscottthePunderWoman
authored andcommitted
feat(router): Add feature provider for enabling hash navigation (#48301)
The argument against this feature: Firstly, this isn't a navigation strategy that's very commonly desired. Secondly, the hash strategy is a location configuration in `@angular/common` and you can use it outside of the Router or with any custom router implementations. So while the net effect of using the router is that it updates the URL which is controlled by the strategy, this feature provider doesn't necessarily need to exist in the Router. The location strategy is an application-wide configuration and affects anything that imports `Location`. Of course, the Router does this in a few places but plenty of other things might as well. The argument for this feature: * Discoverability. While `LocationStrategy` is technically in `@angular/common`, the most common use-case _is_ when using it with the Router. * Precedence in the `RouterModule.forRoot([], {useHash: true})` * Precedence in other routing libraries (`createWebHashHistory` in VueJS and `HashRouter` in React, for example) * The implementation of `withHashLocation` is much more clear than `useHash` was. You can look at the function and see that all it's doing is adding the `HashLocationStrategy` to the providers list. resolves #48295 / #47986 PR Close #48301
1 parent abd11a6 commit f58ad86

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

goldens/public-api/router/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ export interface RouterFeature<FeatureKind extends RouterFeatureKind> {
750750
// @public
751751
export type RouterFeatures = PreloadingFeature | DebugTracingFeature | InitialNavigationFeature | InMemoryScrollingFeature | RouterConfigurationFeature;
752752

753+
// @public
754+
export type RouterHashLocationFeature = RouterFeature<RouterFeatureKind.RouterHashLocationFeature>;
755+
753756
// @public
754757
class RouterLink implements OnChanges, OnDestroy {
755758
constructor(router: Router, route: ActivatedRoute, tabIndexAttribute: string | null | undefined, renderer: Renderer2, el: ElementRef, locationStrategy?: LocationStrategy | undefined);
@@ -1068,6 +1071,9 @@ export function withDisabledInitialNavigation(): DisabledInitialNavigationFeatur
10681071
// @public
10691072
export function withEnabledBlockingInitialNavigation(): EnabledBlockingInitialNavigationFeature;
10701073

1074+
// @public
1075+
export function withHashLocation(): RouterConfigurationFeature;
1076+
10711077
// @public
10721078
export function withInMemoryScrolling(options?: InMemoryScrollingOptions): InMemoryScrollingFeature;
10731079

packages/router/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export {ActivationEnd, ActivationStart, ChildActivationEnd, ChildActivationStart
1515
export {CanActivate, CanActivateChild, CanActivateChildFn, CanActivateFn, CanDeactivate, CanDeactivateFn, CanLoad, CanLoadFn, CanMatch, CanMatchFn, Data, DefaultExport, LoadChildren, LoadChildrenCallback, NavigationBehaviorOptions, OnSameUrlNavigation, QueryParamsHandling, Resolve, ResolveData, ResolveFn, Route, Routes, RunGuardsAndResolvers, UrlMatcher, UrlMatchResult} from './models';
1616
export {Navigation, NavigationExtras, UrlCreationOptions} from './navigation_transition';
1717
export {DefaultTitleStrategy, TitleStrategy} from './page_title_strategy';
18-
export {DebugTracingFeature, DisabledInitialNavigationFeature, EnabledBlockingInitialNavigationFeature, InitialNavigationFeature, InMemoryScrollingFeature, PreloadingFeature, provideRouter, provideRoutes, RouterConfigurationFeature, RouterFeature, RouterFeatures, withDebugTracing, withDisabledInitialNavigation, withEnabledBlockingInitialNavigation, withInMemoryScrolling, withPreloading, withRouterConfig} from './provide_router';
18+
export {DebugTracingFeature, DisabledInitialNavigationFeature, EnabledBlockingInitialNavigationFeature, InitialNavigationFeature, InMemoryScrollingFeature, PreloadingFeature, provideRouter, provideRoutes, RouterConfigurationFeature, RouterFeature, RouterFeatures, RouterHashLocationFeature, withDebugTracing, withDisabledInitialNavigation, withEnabledBlockingInitialNavigation, withHashLocation, withInMemoryScrolling, withPreloading, withRouterConfig} from './provide_router';
1919
export {BaseRouteReuseStrategy, DetachedRouteHandle, RouteReuseStrategy} from './route_reuse_strategy';
2020
export {Router} from './router';
2121
export {ExtraOptions, InitialNavigation, InMemoryScrollingOptions, ROUTER_CONFIGURATION, RouterConfigOptions} from './router_config';

packages/router/src/provide_router.ts

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

9-
import {LOCATION_INITIALIZED, ViewportScroller} from '@angular/common';
9+
import {HashLocationStrategy, LOCATION_INITIALIZED, LocationStrategy, ViewportScroller} from '@angular/common';
1010
import {APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, ApplicationRef, ComponentRef, ENVIRONMENT_INITIALIZER, EnvironmentProviders, inject, InjectFlags, InjectionToken, Injector, makeEnvironmentProviders, NgZone, Provider, Type} from '@angular/core';
1111
import {of, Subject} from 'rxjs';
1212
import {filter, map, take} from 'rxjs/operators';
@@ -580,6 +580,47 @@ export function withRouterConfig(options: RouterConfigOptions): RouterConfigurat
580580
return routerFeature(RouterFeatureKind.RouterConfigurationFeature, providers);
581581
}
582582

583+
/**
584+
* A type alias for providers returned by `withHashLocation` for use with `provideRouter`.
585+
*
586+
* @see `withHashLocation`
587+
* @see `provideRouter`
588+
*
589+
* @publicApi
590+
*/
591+
export type RouterHashLocationFeature = RouterFeature<RouterFeatureKind.RouterHashLocationFeature>;
592+
593+
/**
594+
* Provides the location strategy that uses the URL fragment instead of the history API.
595+
*
596+
* @usageNotes
597+
*
598+
* Basic example of how you can use the hash location option:
599+
* ```
600+
* const appRoutes: Routes = [];
601+
* bootstrapApplication(AppComponent,
602+
* {
603+
* providers: [
604+
* provideRouter(appRoutes, withHashLocation()
605+
* ]
606+
* }
607+
* );
608+
* ```
609+
*
610+
* @see `provideRouter`
611+
* @see `HashLocationStrategy`
612+
*
613+
* @returns A set of providers for use with `provideRouter`.
614+
*
615+
* @publicApi
616+
*/
617+
export function withHashLocation(): RouterConfigurationFeature {
618+
const providers = [
619+
{provide: LocationStrategy, useClass: HashLocationStrategy},
620+
];
621+
return routerFeature(RouterFeatureKind.RouterConfigurationFeature, providers);
622+
}
623+
583624
/**
584625
* A type alias that represents all Router features available for use with `provideRouter`.
585626
* Features can be enabled by adding special functions to the `provideRouter` call.
@@ -602,5 +643,6 @@ export const enum RouterFeatureKind {
602643
EnabledBlockingInitialNavigationFeature,
603644
DisabledInitialNavigationFeature,
604645
InMemoryScrollingFeature,
605-
RouterConfigurationFeature
646+
RouterConfigurationFeature,
647+
RouterHashLocationFeature
606648
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy