0

I have an Angular 2 app hosted on S3. The routing file looks like this.

const APP_ROUTES: Routes = [
  { path: 'checkout', component: CheckoutComponent },
  { path: 'thankyou', component: ThankyouComponent },
  { path: 'lander', component: LanderComponent },
  { path: '', component: LanderComponent }
];

If the user enters www.example.com/checkout I would like it to take them to the checkout route on the app. I figured it would just do this, but S3 is giving me the error No Such Key. Is there a way to do this in Angular 2, or is it something I need to set in S3? I think the problem is S3 is going to the example.com bucket and then expecting a folder checkout. How would I have it not do that and just the app along with whatever route is specified after the URL?

Anton Emery
  • 87
  • 1
  • 4
  • 9

3 Answers3

1
RouterModule.forRoot(APP_ROUTES, { useHash: true })
Praneeth Reddy
  • 424
  • 3
  • 7
0

RouterModule.forRoot(APP_ROUTES)

{ useHash: true } is an option. When you add it, the URL uses Hashes. Without it, no hashes.

0

Angular routing strategies

Add you routes APP_ROUTES to the router using RouterModule.forRoot:

forRoot(routes: Routes, config?: ExtraOptions)

and pass the config as ExtraOptions with useHash: true:

When true, enable the location strategy that uses the URL fragment instead of the history API.

Following table compares the different effects:

useHash routing strategy used e.g. routed as e.g. supposed effect
false PathLocationStrategy (without the hash) /checkout would go directly to S3 bucket and key checkout
true HashLocationStrategy (with the hash symbol) #/checkout would stay inside your Angular app

Angular routing to bypass S3

To achieve this you could create the router as follows:

RouterModule.forRoot(APP_ROUTES, { useHash: true })

This question deals with the same issue: Angular 4 routing --prod mode

See also

Related questions:

hc_dev
  • 8,389
  • 1
  • 26
  • 38