2016-09-24 66 views
3

我是Angular 2的新手,我有一个关于路由的问题。我有一个app.routing文件,其中我只想要一个路径。Angular 2 Empty Component Routing

{path: 'signup', component: SignupComponent} 

但是,如果我运行此代码我得到一个错误:

Error: Uncaught (in promise): Error: Cannot match any routes: '' 

所以我决定'的路径,其中的工作原理完全一样我想上只使用一个空组件。

路由文件:

import {Routes, RouterModule} from '@angular/router'; 
import {SignupComponent} from "./signup/signup.component"; 
import {EmptyComponent} from "./empty/empty.component"; 

const appRoutes:Routes = [ 
    {path: '', component: EmptyComponent}, 
    {path: 'signup', component: SignupComponent} 
]; 

export const appRoutingProviders = []; 

export const routing = RouterModule.forRoot(appRoutes); 

与路由器出口文件:

<header> 
    <div class="btn-wrapper"> 
     <button class="btn-sign-up btn-fancy" routerLink="/signup">Sign Up</button> 
     <button class="btn-sign-in btn-ghost btn-fancy">Sign In</button> 
    </div> 
    <i class="material-icons more">keyboard_arrow_down</i> 
    <router-outlet></router-outlet> 
    <div class="overlay" *ngIf="overlay" (click)="close()"></div> 
    <div class="tinted"></div> 
</header> 

我只想路由器只路由“注册”路径上的SignupComponent。有没有另外一种方法来做到这一点,并消除使用空的组件?如果这个问题写得不好,我很抱歉,我对StackOverflow非常陌生。

回答

4

只是让你空航线重定向到注册路线:

const appRoutes:Routes = [ 
    {path: '', redirectTo: 'signup', pathMatch: 'full'}, 
    {path: 'signup', component: SignupComponent} 
]; 

或者,如果你想,直到你导航到signup路由器的出口是空的,离开四个完全空:

const appRoutes:Routes = [ 
    {path: '', children: []}, 
    {path: 'signup', component: SignupComponent} 
]; 
+0

感谢您的回答!但我不希望任何东西在路径上路由“。 SignupComponent应该只显示在'注册'上。 –

+0

再次感谢,但现在我得到这个错误:无效的路由配置'':必须提供以下之一(组件或redirectTo或儿童或loadChildren) –

+0

哦,真的吗?这对以前的测试版起作用,但可能它不再工作,对不起。你使用哪个版本的角? 2.0.0最终? – nickspoon

0
import { ModuleWithProviders } from '@angular/core'; 
import { Routes, RouterModule } from '@angular/router'; 

import { DemoComponent } from './demo.component'; 

const demoRoutes: Routes = [ 
    { path: '', component: DemoComponent } 
]; 

export const demoRouting: ModuleWithProviders = RouterModule.forChild(demoRoutes);