我正在尝试abgular2 RC5的路由功能,请看下面的代码。
app.component.ts
import { Component,HostBinding } from '@angular/core';
import { ROUTER_DIRECTIVES } from "@angular/router";
@Component({
selector: 'my-app',
template: `
<h1>My First Angular 2 App </h1>
<router-outlet></router-outlet>
`
})
export class AppComponent {}
import { Component } from '@angular/core';
usercomp.ts
@Component({
selector: 'user-comp',
template: `
<h1>USER COMPONENT</h1>
`
})
export class UserComponent {}
homecomponent.ts
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'home-component',
template: `
<h1>Home Component</h1>
`
})
export class HomeComponent {}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { AppComponent } from './app.component';
import { routing } from './approuter'
import { ROUTER_DIRECTIVES } from '@angular/router'
import { HomeComponent } from './homecomponent'
import { UserComponent } from './usercomp'
@NgModule({
imports: [ BrowserModule,routing ],
declarations: [ AppComponent,HomeComponent,UserComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
approuter.ts
import { RouterModule,Routes } from '@angular/router'
const APP_ROUTES:Routes = [
{
path:'user',
component:'UserComponent'
},
{
path:'',
component:'HomeComponent'
}
];
export const routing = RouterModule.forRoot(APP_ROUTES);
ERROR
我尝试在任何地方导出HomeComponent和UserComponent主组件,但无法摆脱此代码。使用RC4它可以很好地使用较旧的方式,但不知道我在这里做了什么错误,请看看,当我误会时让我知道..
答案 0 :(得分:2)
import { RouterModule,Routes } from '@angular/router'
import { HomeComponent } from './homecomponent';
import { UserComponent } from './usercomp';
const APP_ROUTES:Routes = [
{
path:'user',
component: UserComponent
},
{
path:'',
component: HomeComponent
}
];
export const routing = RouterModule.forRoot(APP_ROUTES);
组件接受任何组件类型,检查类引用here
答案 1 :(得分:0)
我认为您需要将pathMatch添加到默认路由:
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', loadChildren: './app/home/home.module#HomeModule' }