1

I have an old app with url formatted this way

localhost/#segment=12

I need to catch these in the new app (angular 7) and redirect to

segment/:id

Really wanted a matcher+redirect in routes to work but matcher takes UrlSegment, not entire url, so all i get is an empty array to work with.

Tried catch all and redirect in controller but id is lost.

The router malformedUriErrorHandler doesn't trigger.

Running out of ideas.

2
  • what about using this.router.url? Commented Oct 22, 2019 at 8:43
  • @Adam it contains UrlSegments, in my case I only get "segment", the id is lost. Commented Oct 22, 2019 at 8:50

2 Answers 2

2

Got it working using route matcher and window.location. Improvements?

const appRoutes: Routes = [
  { matcher: mySegmentMatcher, redirectTo: 'segment/:id' },
  { path: 'segment/:id', component: SegmentComponent },
];

function mySegmentMatcher(url: UrlSegment[]) {
  const test = /#segment=(\d+)$/.exec(location.href);
  if(test && test[1]) {
    const id = test[1];
    url = [new UrlSegment('segment', {} )];
    return { 
      consumed: url,
      posParams: {'id': new UrlSegment(id, {}) }
    }
  }
  return null; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Give a try

import { Router, NavigationStart } from '@angular/router';

constructor(private router: Router) {
router.events.subscribe((event) => {
   if(event instanceof NavigationStart) {
    {
       if(event.url.indexOf('segment')>-1)
     {
        let urlSegment = decodeURIComponent(event.url); 
       //do your split stuff and pass parameters to navigate method
       this.router.navigate(['/segment',12]);
     }
    }
}
});}

2 Comments

Nope, NavigationStart only triggers when navigating inside the app. I'm getting ActivationEnd as first event and no trace of the original url anywhere.
Ok, now I get the events rolling in but the url has already been shopped up and destroyed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.