2017-08-11 77 views
0

我在獲取Angular 2在刷新時或直接輸入時(不通過內部鏈接點擊)加載相同頁面時出現問題。獲取URL重寫,以便與Angular 2正常工作

目前,我對角2路由驗證碼:

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

import { RegistrationComponent } from './registration/registration.component'; 
import { HomepageComponent } from './homepage/homepage.component'; 



const appRoutes: Routes = [ 

    { path: 'register', component: RegistrationComponent }, 
    { path: '', component: HomepageComponent } 
]; 
@NgModule({ 
    imports: [ 
    RouterModule.forRoot(appRoutes) 
    ], 
    exports: [ 
    RouterModule 
    ] 
}) 
export class AppRoutingModule {} 

從URL中移除井號,仍然有它加載一個網頁,我有以下Nginx的重寫:

location/{ 
    try_files $uri $uri/ /index.html; 
} 

每當我嘗試刷新或直接輸入/ register路由,我都會重定向到主頁。有沒有辦法保留在/註冊頁面而不重定向回主頁?

回答

-2

您不需要URL重寫,因爲Angular有HashLocationStrategyPathLocationStrategy

+0

我正在使用PathLocationStrategy,因爲我不想散列(所以沒有HashLocationStrategy),並基於我的理解和職位上的SO,似乎PathLocationStrategy需要在服務器端的東西,因此是Nginx重寫。我錯過了什麼嗎? – freetoplay

+0

它只需要將所有請求傳遞給index.html – be4code

+0

我認爲我正在用上面發佈的nginx重寫指令來做這件事,但問題是現在刷新導致我回到HomeComponent,但我想要留在/註冊頁面上。 – freetoplay