1

I have routing implemented in an Angular 2 app using the default path location strategy. It works fine, except that when reloading a non-default URL path, the component associated with the default route is rendered rather than the component associated with the route path.

For example, http://localhost:5000/locator/bycounty displays the component at http://localhost:5000 when reloading the URL from the server (I have server-side routing set to resolve all static paths to the same file).

Is that expected behavior or is my app misbehaving?

gxclarke
  • 1,953
  • 3
  • 21
  • 42
  • The information you provided is not enough to diagnose the problem. – Günter Zöchbauer Mar 22 '16 at 04:36
  • I am not asking for the problem to be diagnosed. I am trying to determine if this is the expected behavior of the angular 2 router. Are you implying that the router should automatically traverse the routes from the URL path when the page is initially loaded? – gxclarke Mar 22 '16 at 16:02
  • I'm implying that your question doesn't provide enough information to discuss if it's a bug or feature of Angular or your code ;-) – Günter Zöchbauer Mar 22 '16 at 16:15
  • Perhaps I have phrased my question poorly because my intent it to ask a simple question about the capabilities of the Angular 2 router. Try this instead -- is it a built-in feature or capability of the angular 2 router that the URL path is automatically parsed on page load to render the view associated with the matching route? I ask this because I am finding that the URL path is ignored by the router on page load, and instead the view associated with the default route is displayed. – gxclarke Mar 22 '16 at 20:03
  • The router is responsible to interpret the path and navigate to this route. If it navigates to the default route this is probably because the passed route couldn't be interpreted. Is this what your question is about? – Günter Zöchbauer Mar 22 '16 at 20:24
  • Yes, it is. Thank you for your patience. I am just refreshing the page from a valid route, so I don't know why it would be considered invalid when the path is interpreted at page load. For example, initial load is "/", navigate via app (routerLink) to "/a/b", hit F5 in browser -- url path still shows "/a/b" but view is for "/". No errors in console. – gxclarke Mar 22 '16 at 20:53
  • Then it's probably http://stackoverflow.com/questions/31415052/angular-2-0-router-not-working-on-reloading-the-browser – Günter Zöchbauer Mar 23 '16 at 05:16
  • It isn't that. I have the web server mapping all requests to the main index.html. If it was that, then I'd see a 404 response or something like that, but my app is loading and initializing fine, which confirms the right page is being sent from the server. The client router is just not parsing or acting on the path at initialization. – gxclarke Mar 23 '16 at 14:58

1 Answers1

1

The answer is that the app is misbehaving. The reason is because I had set the base dynamically on initialization rather than using a simple <base href="/" />.

bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    provide(APP_BASE_HREF, { useValue: location.pathname }) // <= BAD
]);

I had copied the provide(....) line of code from somewhere at one point when trying to fix a different router problem. This line sets the root path for the router dynamically based on the URL path at load time. That's why on initial load I always saw the default route rather than the one represented by the URL path.

gxclarke
  • 1,953
  • 3
  • 21
  • 42