Skip to content

Instantly share code, notes, and snippets.

@ranwahle
Last active June 9, 2019 19:50
Show Gist options
  • Save ranwahle/b90b6d7a347dffa08ddc3355eccc851e to your computer and use it in GitHub Desktop.
Save ranwahle/b90b6d7a347dffa08ddc3355eccc851e to your computer and use it in GitHub Desktop.
Guard on Router-outlet
async changeRoute(newRoute) {
const treeBuilder = module.Router.router.routingSnapshotTreeBuilder
const router = module.Router.router
const newRouteData = treeBuilder.buildRouteTree(newRoute)
if (!newRouteData) {
throw Error(`Could not build tree for ${newRoute}`)
}
const canGoOn = await router.canGoOn(window.location.pathname, newRouteData.guard)
if (!canGoOn) {
history.back()
return
}
router.currentSnapshot = newRouteData
this.clearChildren()
// To avoid error, set the attributes member to be empty object, if null or undefined
if (!newRouteData.attributes) {
newRouteData.attributes = {}
}
const newElement = document.createElement(newRouteData.element, newRouteData.attributes)
Object.keys(newRouteData.attributes).forEach(key => {
newElement.setAttribute(key, newRouteData.attributes[key])
})
this.appendChild(newElement)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment