2016-12-05 216 views
1

我所有,刷新頁面

時Angular2路由器的問題我有一個很常見的問題,但我沒有找到解決辦法,真正需要你的這一個幫助。

我在apache服務器上有一個angular2應用程序,並且在延遲加載的模塊中存在路由器問題。

我有一個根模塊,這是他的路由器文件:

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

// App Components 
import { AppComponent } from './app.component'; 
import { LoginComponent } from './auth/login.component'; 


const ROUTES: Routes = [ 
    { path: '', redirectTo: '/home', pathMatch: 'full' }, 
    { path: 'home', 
     loadChildren: 'dist/home/home.module.js#HomeModule' 
    } 
    { 
     path: 'childapp', 
     loadChildren: 'dist/childapp/childapp.module.js#ChildappModule' 

    }, 
    { 
     path: 'login', 
     component: LoginComponent 
    }, 
    { path: '**', redirectTo: '/home'} 
]; 

@NgModule({ 
    imports: [ RouterModule.forRoot(ROUTES) ], 
    exports: [ RouterModule ] 
}) 
export class RoutingModule {} 

這裏是一個子單元的路由爲例:

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

import { ChildappComponent} from './childapp.component'; 
import { Sub1Component} from './sub1/Sub1.component'; 
import { Sub2Component} from './sub2/Sub2.component'; 

const routes = [ 
    { 
     path: "", 
     component: ChildappComponent, 

     children: [ 
      {path: "sub2", 
      component: Sub2Component}, 
      {path: "", 
      component: Sub1Component}, 
     ] 
    }, 

]; 

@NgModule({ 
    imports: [ RouterModule.forChild(routes) ], 
    exports: [ RouterModule ] 
}) 
export class LocobookRoutingModule {} 

當我點擊鏈接,應有盡有沒問題。

當我刷新「http://mywebsite.com/app/home」或「http://mywebsite.com/app/childapp」時,一切正常。

但是,當我嘗試刷新「http://mywebsite.com/app/childapp/sub2」時,頁面不顯示。 如果有幫助,這是我的.htacces文件:

RewriteEngine on 

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# otherwise forward it to index.php 
RewriteRule . index.php 

非常感謝您對這個幫助。

回答

0

我找到了解決我的問題的方法。

因爲我使用的Apache和PHP,我可以很容易地指定一個絕對在我的index.php文件:

<base href="<?= "http://".$_SERVER['HTTP_HOST'].str_replace("index.php", "", $_SERVER['SCRIPT_NAME']); ?>"> 

我希望這會幫助別人!