1
1
import { ComponentFactory , ComponentFactoryResolver } from '@angular/core' ;
2
2
import { Location } from '@angular/common' ;
3
-
4
3
import { App } from '../components/app/app' ;
5
4
import { DIRECTION_BACK , NavLink , NavSegment , TransitionDoneFn , convertToViews , isNav , isTab , isTabs } from './nav-util' ;
6
5
import { ModuleLoader } from '../util/module-loader' ;
@@ -27,23 +26,24 @@ export class DeepLinker {
27
26
public _serializer : UrlSerializer ,
28
27
public _location : Location ,
29
28
public _moduleLoader : ModuleLoader ,
30
- public _baseCfr : ComponentFactoryResolver
29
+ public _baseCfr : ComponentFactoryResolver ,
30
+ public _baseHref : string
31
31
) { }
32
32
33
33
/**
34
34
* @internal
35
35
*/
36
36
init ( ) {
37
37
// scenario 1: Initial load of all navs from the initial browser URL
38
- const browserUrl = normalizeUrl ( this . _location . path ( ) ) ;
38
+ const browserUrl = normalizeUrl ( this . _location . path ( ) , this . _baseHref ) ;
39
39
console . debug ( `DeepLinker, init load: ${ browserUrl } ` ) ;
40
40
41
41
// remember this URL in our internal history stack
42
42
this . _historyPush ( browserUrl ) ;
43
43
44
44
// listen for browser URL changes
45
45
this . _location . subscribe ( ( locationChg : { url : string } ) => {
46
- this . _urlChange ( normalizeUrl ( locationChg . url ) ) ;
46
+ this . _urlChange ( normalizeUrl ( locationChg . url , this . _baseHref ) ) ;
47
47
} ) ;
48
48
}
49
49
@@ -120,7 +120,7 @@ export class DeepLinker {
120
120
121
121
getCurrentSegments ( browserUrl ?: string ) {
122
122
if ( ! browserUrl ) {
123
- browserUrl = normalizeUrl ( this . _location . path ( ) ) ;
123
+ browserUrl = normalizeUrl ( this . _location . path ( ) , this . _baseHref ) ;
124
124
}
125
125
return this . _serializer . parse ( browserUrl ) ;
126
126
}
@@ -290,7 +290,7 @@ export class DeepLinker {
290
290
* @internal
291
291
*/
292
292
getSegmentByNavIdOrName ( navId : string , name : string ) : NavSegment {
293
- const browserUrl = normalizeUrl ( this . _location . path ( ) ) ;
293
+ const browserUrl = normalizeUrl ( this . _location . path ( ) , this . _baseHref ) ;
294
294
const segments = this . _serializer . parse ( browserUrl ) ;
295
295
for ( const segment of segments ) {
296
296
if ( segment . navId === navId || segment . navId === name ) {
@@ -433,19 +433,22 @@ export class DeepLinker {
433
433
}
434
434
435
435
436
- export function setupDeepLinker ( app : App , serializer : UrlSerializer , location : Location , moduleLoader : ModuleLoader , cfr : ComponentFactoryResolver ) {
437
- const deepLinker = new DeepLinker ( app , serializer , location , moduleLoader , cfr ) ;
436
+ export function setupDeepLinker ( app : App , serializer : UrlSerializer , location : Location , moduleLoader : ModuleLoader , cfr : ComponentFactoryResolver , baseHref : string ) {
437
+ const deepLinker = new DeepLinker ( app , serializer , location , moduleLoader , cfr , baseHref ) ;
438
438
deepLinker . init ( ) ;
439
439
return deepLinker ;
440
440
}
441
441
442
442
443
- export function normalizeUrl ( browserUrl : string ) : string {
443
+ export function normalizeUrl ( browserUrl : string , baseHref : string = '/' ) : string {
444
444
browserUrl = browserUrl . trim ( ) ;
445
445
if ( browserUrl . charAt ( 0 ) !== '/' ) {
446
446
// ensure first char is a /
447
447
browserUrl = '/' + browserUrl ;
448
448
}
449
+ if ( ! browserUrl . startsWith ( baseHref ) ) {
450
+ browserUrl = baseHref + browserUrl ;
451
+ }
449
452
if ( browserUrl . length > 1 && browserUrl . charAt ( browserUrl . length - 1 ) === '/' ) {
450
453
// ensure last char is not a /
451
454
browserUrl = browserUrl . substr ( 0 , browserUrl . length - 1 ) ;
0 commit comments