2016-12-31 124 views
4

試圖在我的angular2應用中創建我的重定向路由。Angular2重定向路由不會改變瀏覽器網址

但我的問題是,當有人輸入了「NOPATH」,那麼用戶會被重定向到「HomeComponent」,但網址仍然保持「/#/ NOPATH」

路徑無效,我想redirectTo路線也更改網址! 我應該如何實現這一目標?

我應該把一個如果在我的HomeComponent構造函數,檢查當前的網址,並改變他的路線homecomponent?或者有什麼我失蹤?

路線:

const routes: Routes = [ 
    { path: '', pathMatch: 'full', component: HomeComponent }, 
    { path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] }, 
    { path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] }, 
    { path: 'users', component: UserComponent, canActivate: [AuthGuard] }, 
    { path: '**', redirectTo: '' } 
]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(routes, {useHash: true}); 

編輯:

想這一點,但我沒有得到重定向到主成分

const routes: Routes = [ 
    { path: '', pathMatch: 'full' , redirectTo: 'home' }, 
    { path: 'home', component: HomeComponent }, 
    { path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] }, 
    { path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] }, 
    { path: 'users', component: UserComponent, canActivate: [AuthGuard] }, 
    { path: '**', redirectTo: '' } 
]; 
+0

也許不使用重定向路由,只需使用「**」,組件:HomeComponent(而不是重定向)。 (NM嘗試了幾種方法,是的,我不會這樣做)。 –

+0

你使用的是最新的Angular2和路由器版本嗎?最近我看到了類似的問題,在更新解決它的地方。 –

+0

我使用3.2.3版本的路由器,我不知道如何檢查我是否有最新版本 – Vince

回答

3

現在,我沒有找到任何更好的w比黑客Angular2路由器。

這是我的解決方案,它不是一個美麗的方式來解決問題......但至少它工作,因爲我想!

路線:

const routes: Routes = [ 
    { path: '', pathMatch: 'full', component: HomeComponent }, 
    { path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] }, 
    { path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] }, 
    { path: 'users', component: UserComponent, canActivate: [AuthGuard] }, 
    { path: '**', component: HomeComponent, canActivate: [RedirectToHomeGuard] } 
]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(routes, { useHash: true }); 

RedirectToHomeGuard:

@Injectable() 
export class RedirectToHomeGuard implements CanActivate { 

    constructor(private router: Router) { } 

    canActivate() { 
    this.router.navigate(['/']); 
    return false; 
    } 
} 

你們認爲這可能是一個很好的解決方案?

0

你應該試試這個,

const routes: Routes = [ 
    { path: '', pathMatch: 'full', redirectTo:'home'}, 
    //<<<-----added redirectTo attribute 

    { path: 'home', component: HomeComponent },     
    //<<<----added this line 


    { path: 'login', component: LoginComponent, canActivate: [AnonymousGuard] }, 
    { path: 'register', component: RegisterComponent, canActivate: [AnonymousGuard] }, 
    { path: 'users', component: UserComponent, canActivate: [AuthGuard] }, 

    { path: '**', component: HomeComponent } //<<<--- updated line 
]; 
+0

它不起作用,它保留了網址中的nopath,我沒有被重定向到HomeComponent – Vince

+0

我也嘗試了redirectTo:'home',並且它在瀏覽器中保留了錯誤的url,我需要輸入push進入第二次,所以它改變了網址bu重新加載應用程序完全... – Vince

+0

**我需要推入第二次輸入瀏覽器的網址,使應用程序重新加載和工作..但這是一個奇怪的行爲,我希望我的用戶編寫一個不好的url被重定向到我的家庭組件與主頁的URL,而無需重新加載應用程序 – Vince

0

也許是爲時已晚,但我認爲問題應該與基礎href標記。我在我的index.html中使用了類似下面的代碼:

<head> 
    <meta charset="utf-8"> 
    <title>MyCoolApp</title> 
    <base href="."> 
</head> 

基本上使用href =「。」打破了路由。相反,如果你使用href =「/」來做這個伎倆。

<head> 
    <meta charset="utf-8"> 
    <title>MyCoolApp</title> 
    <base href="/"> 
</head> 

所以,現在的路由作品和所有的非匹配的路由將結束在「**」的路徑和自「/」作爲基本URL找到* -bundle.js文件,他們將找到。我認爲這就是爲什麼當你導航到「/」時,一切都可以再次運作。

相關問題