这是一个用UrlHandlingStrategy
重定向特定URL的好策略,还是有更好的方法?
例如,Below类实现UrlHandlingStrategy
,并且将重定向包含#(散列)的ngJs URL.
下面的方法行得通,但我感觉还有更好的方法,问题是class::shouldProcessUrl
被多次调用.
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;
}
}