/
router.tsx
71 lines (60 loc) · 1.68 KB
/
router.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Component, Prop, PropDidChange, State } from '@stencil/core';
import createHistory from '../../utils/createBrowserHistory';
import { ActiveRouter, LocationSegments, MatchResults } from '../../global/interfaces';
/**
* @name Router
* @module ionic
* @description
*/
@Component({
tag: 'stencil-router'
})
export class Router {
@Prop() root: string = '/';
// A suffix to append to the page title whenever
// it's updated through RouteTitle
@Prop() titleSuffix: string = '';
@PropDidChange('titleSuffix')
titleSuffixChanged(newValue: string) {
this.activeRouter.set({
titleSuffix: newValue
});
}
@Prop({ context: 'activeRouter' }) activeRouter: ActiveRouter;
unsubscribe: Function = () => {};
@State() match: MatchResults | null = null;
computeMatch(pathname?: string) {
return {
path: this.root,
url: this.root,
isExact: pathname === this.root,
params: {}
} as MatchResults;
}
componentWillLoad() {
const history = createHistory();
history.listen((location: LocationSegments) => {
this.activeRouter.set({ location });
});
this.activeRouter.set({
location: history.location,
titleSuffix: this.titleSuffix,
history
});
// subscribe the project's active router and listen
// for changes. Recompute the match if any updates get
// pushed
this.unsubscribe = this.activeRouter.subscribe(() => {
this.match = this.computeMatch();
});
this.match = this.computeMatch();
}
componentDidUnload() {
// be sure to unsubscribe to the router so that we don't
// get any memory leaks
this.unsubscribe();
}
render() {
return <slot />;
}
}