diff --git a/lib/component.js b/lib/component.js index 6419baf5..11ce9a2e 100644 --- a/lib/component.js +++ b/lib/component.js @@ -381,8 +381,17 @@ class Component extends WebComponent { Object.assign(this.state, this.getJSONAttribute(`data-state`), this._stateFromAttributes()); if (Object.keys(this.getConfig(`routes`)).length) { - this.router = new Router(this, {historyMethod: this.historyMethod}); - this.navigate(window.location.hash); + const locationContext = this.hasContext(`location`) ? this.getContext(`location`) : window.location; + const historyContext = this.hasContext(`history`) ? this.getContext(`history`) : window.history; + + const windowContext = { + location: locationContext, + history: historyContext, + addEventListener: window.addEventListener.bind(window), + removeEventListener: window.removeEventListener.bind(window), + }; + this.router = new Router(this, {historyMethod: this.historyMethod, window: windowContext}); + this.navigate(locationContext.hash); } for (const contextName of this.getConfig(`contexts`)) { @@ -835,6 +844,10 @@ class Component extends WebComponent { return availableContexts[contextName]; } + + hasContext(contextName) { + return this._contexts.has(contextName); + } } export default Component;