Last active
June 9, 2019 19:50
-
-
Save ranwahle/b90b6d7a347dffa08ddc3355eccc851e to your computer and use it in GitHub Desktop.
Guard on Router-outlet
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
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