Created
June 26, 2020 11:36
-
-
Save velotiotech/d1b6ececcff1b72734269ccce23c0ac6 to your computer and use it in GitHub Desktop.
Utility to render MicroFrontend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function renderMicroFrontend(pathname) { | |
const microFrontend = routes[pathname || window.location.hash]; | |
const root = document.getElementById('microfrontend'); | |
root.innerHTML = microFrontend ? new microFrontend().render(): new Home().render(); | |
$(window).scrollTop(0); | |
} | |
$(window).bind( 'hashchange', function(e) { renderFrontend(window.location.hash); }); | |
renderFrontend(window.location.hash); | |
utility routes.js (A map of the hash route to the Microfrontend class) | |
const routes = { | |
'#': Home, | |
'': Home, | |
'#home': Home, | |
'#cart': Cart, | |
'#checkout': Checkout, | |
'#product': Product, | |
'#contact': Contact, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment