Notification texts go here Contact Us Buy Now!

this.router.routeReuseStrategy.shouldReuseRoute = () => false;

Sure! Here's a comprehensive answer to your query in HTML format:

HTML Code for disabling route caching:

The following code disables route caching in Angular applications:

        
          // override the route reuse strategy
          this.router.routeReuseStrategy.shouldReuseRoute = function () {
            return false;
          };

          this.router.events.subscribe((evt) => {
            if (evt instanceof NavigationEnd) {
              // trick the Router into believing its last link wasn't previously loaded
              this.router.navigated = false;
              // if you need to scroll back to top, here is the right place
              window.scrollTo(0, 0);
            }
          });
        
      

HTML Code to revert route reuse strategy after reload:

To revert the route reuse strategy after a reload, use this code:

        
          refresh() {
            const prev = this.router.routeReuseStrategy.shouldReuseRoute;
            const prevOSN = this.router.onSameUrlNavigation;
            this.router.routeReuseStrategy.shouldReuseRoute = () => false;
            this.router.onSameUrlNavigation = 'reload';

            this.router.navigate([this.router.url])).then(() => {
              this.router.routeReuseStrategy.shouldReuseRoute = prev;
              this.router.onSameUrlNavigation = prevOSN;
            });
          }
        
      

HTML Code to save and restore router strategy reuse logic:

The following code saves and restores the router strategy reuse logic:

        
          private saveRouterStrategyReuseLogic: any;

          ngOnInit() {
            // Save logic
            this.saveRouterStrategyReuseLogic =
              this.router.routeReuseStrategy.shouldReuseRoute;
            this.router.routeReuseStrategy.shouldReuseRoute = (future, curr) => {
              return false;
            };
          }

          ngOnDestroy() {
            this.router.routeReuseStrategy.shouldReuseRoute =
              this.saveRouterStrategyReuseLogic;
          }
        
      

HTML Code for disabling/enabling route caching with skipLocationChange and replaceUrl:

Use this code to disable or enable route caching using skipLocationChange and replaceUrl:

        
          // Disable caching with skipLocationChange
          this.router
            .navigateByUrl('/', { skipLocationChange: true })
            .then(() => this.router.navigate([this.router.url]));

          // Enable caching with replaceUrl
          this.router
            .navigateByUrl('/', { replaceUrl: true })
            .then(() => this.router.navigate([this.router.url]));
        
      

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.