刷新页面时Angular2路由器出现问题

时间:2016-12-05 12:49:24

标签: .htaccess angular angular2-routing

我都是,

我有一个非常普遍的问题,但我找不到解决方案,真的需要你的帮助。

我在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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法。

因为我使用的是Apache和PhP,所以我可以在index.php文件中轻松指定一个绝对值:

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

我希望它会帮助别人!

相关问题