Issue
Is this a good strategy to redirect certain URL with UrlHandlingStrategy
or is there a better way?
e.g. the bellow class implements UrlHandlingStrategy
and will redirect ngJs url that contains a #(hash).
The below works but I get the feeling there is a better way and the issue is the class::shouldProcessUrl
is called multiple times.
export class HybridUrlHandlingStrategy implements UrlHandlingStrategy {
constructor(private readonly location: Location) {}
shouldProcessUrl(urlTree: UrlTree): boolean {
const url: string = urlTree.toString();
// handle #hash routing
if (urlTree.fragment) {
const fragment: string = urlTree.fragment.replace(/[#!]/g, '');
this.location.go(fragment); // <------------------------------- redirect done here
return false;
}
return /^\/(errors|angularIO).*$/.test(url);
}
extract(urlTree: UrlTree): UrlTree {
return urlTree;
}
merge(urlTree: UrlTree, _rawUrl: UrlTree): UrlTree {
return urlTree;
}
}
Solution
If you ask if this is the right place then NO, because below is what is mentioned in the documentation.
shouldProcessUrl() - Tells the router if this URL should be processed.
abstract shouldProcessUrl(url: UrlTree): boolean
So instead, its better you perform the navigation logic on a guard
rather than on the UrlHandlingStrategy
Reference Stack Overflow Answer
Answered By - Naren Murali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.