【问题标题】:Angular Html5Mode PlayFramework gives 404 when refreshedAngular Html5Mode PlayFramework 在刷新时给出 404
【发布时间】:2017-02-27 20:28:47
【问题描述】:

我正在设置一个Play Framework 2.5 服务器以使用Angular 的 SPA(单页应用程序)方法,并希望使用删除 HashTags(#example)的PathLocationStrategy

当我第一次访问服务器时页面加载正常,但如果我刷新或在添加书签后返回,会给出404

在 Angular documentation for Angular Router 中声明需要服务器端配置

有很多关于如何使用nginxapache 执行此操作的信息,但是在没有代理的情况下如何在 Play 中执行此操作?

这是我的角度路由器配置:

const routes: Routes = [
    {
        path: 'main',
        component: JobsComponent
    },
    {
        path: 'job-details',
        component: JobDetailsComponent
    },
    {
        path: 'job-submit',
        component: JobSubmitComponent
    },
    {
        path: '',
        redirectTo: '/main',
        pathMatch: 'full'
    },
    {
        path: '**',
        redirectTo: '/main',
    }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule],
    providers: []
})
export class AppRoutingModule { }

播放routes.conf

GET     /    controllers.Assets.at(path="/public", file="index.html")

【问题讨论】:

    标签: playframework angular2-routing playframework-2.5


    【解决方案1】:

    发生的情况是,当 Angular 将 URL 栏更改为 /main 时,您的服务器不涉及。如果你按下刷新,浏览器会请求/main,它不会找到。

    您最好的方法是创建一条能够捕获所需路线并将其发送回索引的路线。这是一个例子

    制作捕获请求的路由:

    也可以是 Play 2.5 documentation 的正则表达式

    # Place at the bottom of your routes.conf to be checked last
    GET     /*anyUrl     controllers.CatchAllController.catchAll(anyUrl)
    

    制作控制器

    package controllers
    
    import play.api.mvc.{Action, Controller}
    
    import scala.concurrent.ExecutionContext.Implicits.global
    
    class CatchAllController extends Controller{
        def catchAll(anyUrl: String) = Action.async { request =>
            Assets.at("/public", "index.html").apply(request)
        }
    }
    

    所有链接/刷新都应按预期工作,无需代理

    【讨论】:

      猜你喜欢
      • 2017-01-20
      • 2015-10-04
      • 2015-04-21
      • 1970-01-01
      • 2017-12-01
      • 2018-12-26
      • 2016-05-30
      • 2016-09-13
      • 1970-01-01
      相关资源
      最近更新 更多