路由在 Angular2 中不起作用

Routing not working in Angular2

我的路由在 Angular2 中不起作用,为了证明这一点,我将相同的组件作为我网站根目录和 /login 的目标。该组件在 http://localhost:3000 工作,但在 http://localhost:3000/login,我只收到一条通知 "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';
}

这是我的 index 文件的 link。 我做错了什么?

两条路线合二为一@RouteConfig(...)不能同名:

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

您应该将 ROUTER_PROVIDERS 移动到 bootstrap()(如 HTTP_PROVIDERS