2017-05-26 52 views
0

我想實現我的angular4應用簡單的路由deeplinking,但有一個問題與深層鏈接不工作..如何啓用angular4

例如,我有一個關於組件和主頁(待辦事項)組件,我app.routing.ts文件如下:

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

import { TodosComponent } from './todos/todos.component'; 
import { AboutComponent } from './about/about.component'; 
import { CallbackComponent } from './callback/callback.component'; 

const appRoutes: Routes = [ 

    { 
    path: '', 
    component: TodosComponent 
    }, 
    { 
    path: 'about', 
    component: AboutComponent 
    }, 
    { 
    path: 'callback', 
    component: CallbackComponent 
    } 

]; 

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); 

現在導航工程,當我點擊從routerLink應用這些組件,但如果我直接輸入網址到瀏覽器,我得到一個404 ..

這是打破我的回調從我驗證來源:(

我的app.module.ts文件已導入路由器文件,但這仍然無法正常工作..我如何啓用深連接在這裏?

+2

提示:404來自**服務器**。當您在地址欄中輸入時,您正在向服務器發送請求。 Angular在這裏無關緊要。閱讀https://stackoverflow.com/questions/26894769/understanding-what-it-takes-to-remove-the-hash-from-angular-routes/26897659#26897659。 –

+0

https://angular.io/docs/ts/latest/guide/deployment.html#!#fallback – yurzui

回答

0

如果你有沒有孩子空路徑路由'',你應該使用pathMatch: 'full'

{ 
    path: '', 
    component: TodosComponent, 
    pathMatch: 'full' 
    }, 

否則每一條路徑匹配,路由器會搜索匹配的(不存在的)子路由與其餘路徑。

正如評論中所述,如果您使用PathLocationStrategy(默認值),則需要配置服務器以支持它。

您可以檢查服務器是否是通過切換到HashLocationStrategy

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

的路由看起來不同的罪魁禍首,所以不要指望老路線(不#)時,則直接進入他們的工作。