2

There are some similar questions but I did not find any solution for this problem yet.

Lets assume I use

pub serve --port 13371

in order to start my Angular 2 / Dart application locally. In the base file I have a router configured like this:

@RouteConfig(const [
  const Route(
      path: '/home',
      name: 'Home',
      component: HomeComponent,
      useAsDefault: true
  ),
  const Route(
      path: '/about',
      name: 'About',
      component: AboutComponent
  )
])
class AppComponent {}

and a template like this:

    <nav>
      <ul>
        <li>
          <a [routerLink]="['Home']">Home</a>
        </li>
        <li>
          <a [routerLink]="['About']">About</a>
        </li>
      </ul>
    </nav>

In Dartium or another Browser I go to http://localhost:13371/ which redirects me to http://localhost:13371/home. Whenever I press reload now I get a 404 error.

The reason of this is the server configuration. When angular changes the URL it knows where to go and how to rewrite the URL. But for the server /home or /about does not exist.

However my question is how can I configure pub in order to redirect always to index.html so that I can refresh and get forwarded to subsite then? Is there some configuration to add to pubspec.yaml?

2 Answers 2

4

Either add

bootstrap(AppComponent, [
  /* other providers */, 
  provide(LocationStrategy, useClass: HashLocationStrategy),
])

or use a proxy like Nginx that supports rewriting. pub itself doesn't support it in any way.

See also https://pub.dartlang.org/packages/pub_serve_rewrites

0

Do not use a slash on your path "/". Try this:

const userRoutes: Routes = [
    { path: 'home', component: HomeComponent, pathMatch: 'full' },
];
1
  • That doesn't apply to the router used in Dart Angular 2/3. Jan 31, 2017 at 21:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.