2

My Routing isn't working in Angular2, to demonstrate the point, I have put the same component as the destination for both the root of my site and /login. The component works at http://localhost:3000, but at http://localhost:3000/login, I just get a notice "Cannot GET /login".

app.component.ts:

import { Component } from 'angular2/core';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';

import {TodoService} from './todo/services/todo.service';
import { TodoCmp } from './todo/components/todo.component';
import { LoginComponent } from './user/components/login.component';
import { UserService } from './user/services/user.service';


@Component({
  selector: 'my-app',
  template: `
    <h1>{{title}}</h1>
    <router-outlet></router-outlet>
  `,
  styleUrls: ['client/dev/todo/styles/todo.css'],
  directives: [ROUTER_DIRECTIVES],
  providers: [
    ROUTER_PROVIDERS,
    TodoService
  ]
})
@RouteConfig([
  {
    path: '/',
    name: 'TodoCmp',
    component: TodoCmp,
    useAsDefault: true
  },
  {
    path: '/login',
    name: 'TodoCmp',
    component: TodoCmp
  }
])

export class AppComponent {
  title = 'ng2do';
}

Here is a link to my index file. What have I done wrong?

George Edwards
  • 8,979
  • 20
  • 78
  • 161
  • have you included all necessary library files? – sdfacre Mar 24 '16 at 05:17
  • @sdfacre Yes, I was facing an issue with my system.config not adding file extensions to dependencies. That is now resolved, please see my updated question. – George Edwards Mar 24 '16 at 14:35
  • if everything is loaded correctly, why it still complains 'require is not defined'...sorry, I don't have any clue unless you can provide a plunker. Have you followed any example? like http://www.waynehong.com/javascript/angular-2-routing-example-typescript/ – sdfacre Mar 25 '16 at 03:04
  • please include your index.html – Rick Mar 25 '16 at 10:42
  • @Rick [Here](https://github.com/georgeedwards/Gen-App/blob/master/client/dev/index.html) is a link, I'll add it to the question – George Edwards Mar 25 '16 at 11:49

1 Answers1

0

Two routes in one @RouteConfig(...) can't have the same name:

@RouteConfig([
  {
    path: '/',
    name: 'TodoCmp',
    component: TodoCmp,
    useAsDefault: true
  },
  {
    path: '/login',
    name: 'TodoCmp',  <!-- <<<== should be 'Login' instead of 'TodoCmp'
    component: TodoCmp
  }
])

You should move ROUTER_PROVIDERS to bootstrap() (like HTTP_PROVIDERS)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567