Angular 2 rc3路由问题

时间:2016-06-26 17:20:15

标签: angular angular2-routing

我想我已按照指示在rc3工作中获取路由。它在rc1中运行良好。我创建app.routes,app.providers,它看起来像这样:

main.ts:

import {bootstrap}    from '@angular/platform-browser-dynamic';
import {APP_ROUTER_PROVIDER} from './app.routes';
import {HTTP_PROVIDERS} from '@angular/http';
import {AppComponent} from './app.component';

bootstrap(AppComponent, [APP_ROUTER_PROVIDER, HTTP_PROVIDERS]); 

app.component.ts:

import {Component, OnInit} from '@angular/core';
import {ROUTER_DIRECTIVES, Router} from '@angular/router';
import { APP_PROVIDERS } from './app.providers';


@Component({
    selector: 'my-app',
    template: '<router-outlet></router-outlet>',
     directives: [ROUTER_DIRECTIVES],
    providers: [APP_PROVIDERS]
})
export class AppComponent implements OnInit { 
    constructor(private router: Router) {


    }
    ngOnInit() {
        this.router.navigate(['home']);
    }
}

app.providers.ts

import { bind } from '@angular/core';
import { HTTP_PROVIDERS } from '@angular/http';
import { FORM_PROVIDERS, LocationStrategy, HashLocationStrategy } from '@angular/common';
   
export const APP_PROVIDERS = [
    
    FORM_PROVIDERS,
    HTTP_PROVIDERS 
];

home.routes.ts

import {RouterConfig} from '@angular/router';
import {HomeComponent} from './home.component';

export const HomeRoutes: RouterConfig = [
    { path: 'home', component: HomeComponent, terminal: true }
];

home.component.ts

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES, Router} from '@angular/router';

@Component({
    selector: 'kg-home',
    templateUrl: './home.component.html',
    directives: [ROUTER_DIRECTIVES]
})

export class HomeComponent {
    constructor(private router: Router) {
        console.log('in home component')
    }
}

但是当我运行它时,我得到: enter image description here

任何帮助都会受到赞赏,我现在已经盯着这些东西2天了。

1 个答案:

答案 0 :(得分:1)

您需要默认路线

export const HomeRoutes: RouterConfig = [
    { path: '', redirectTo: '/home', terminal: true},
    { path: 'home', component: HomeComponent }
];