feat(smartrouter): Add destroy method to SmartRouter class.
This commit is contained in:
@@ -41,6 +41,11 @@ export class SmartRouter {
|
||||
*/
|
||||
private basePath: string;
|
||||
|
||||
/**
|
||||
* Reference to the event listener function for cleanup
|
||||
*/
|
||||
private popstateListener: (event: PopStateEvent) => void;
|
||||
|
||||
/**
|
||||
* Creates an instance of Router.
|
||||
*/
|
||||
@@ -53,10 +58,11 @@ export class SmartRouter {
|
||||
this.basePath = basePath;
|
||||
|
||||
// lets subscribe to route changes
|
||||
window.addEventListener('popstate', (popStateEventArg) => {
|
||||
this.popstateListener = (popStateEventArg) => {
|
||||
popStateEventArg.preventDefault();
|
||||
this._handleRouteState();
|
||||
});
|
||||
};
|
||||
window.addEventListener('popstate', this.popstateListener);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,4 +125,14 @@ export class SmartRouter {
|
||||
} as IRouteInfo); // not waiting here
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the router instance, removing all external references
|
||||
*/
|
||||
public destroy() {
|
||||
// Remove the event listener for popstate
|
||||
window.removeEventListener('popstate', this.popstateListener);
|
||||
// Clear all routes
|
||||
this.routes = [];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user