如何使页面刷新时使用App Engine的Angular 2路由工作? [英] How do I make Angular 2 routing work with App Engine on page refresh?

查看:85
本文介绍了如何使页面刷新时使用App Engine的Angular 2路由工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在App Engine标准环境中运行Angular 2应用程序。它适用于在应用程序内导航时使用以下 app.yaml 配置:

 处理程序:
- url:/api/.*
脚本:_go_app
$ b - url:(。*)/
static_files:static \ 1 / index.html
上传:static

- url:(。*)
static_files:static\1
上传:static
>

我可以点击 / /客户端 / clients / 234234 ,它可以正常工作。 然而,如果我刷新浏览器在非基本路径例如 http://myapp.appspot.com/clients/234234 然后我得到一个404错误。我想我需要为所有路径服务我的 index.html ,这就是我认为的(。*)/ (。*)会做。



如何设置我的处理程序/应用程序,以便使用HTML5路由而不是让这发生?

解决方案

我有一堆需要服务的静态文件,所以我先添加了它们的映射。我也(最重要的)改变了index.html的服务方式:

 处理程序:
- url:/ api /.*
脚本:_go_app
$ b $ - url:/(.*\\.svg)
static_files:static / \ 1
upload:static /( 。* \.svg)

- url:/(.*\.js)
static_files:static / \ 1
上传:static /(.* \\ \\.js)
$ b $ url - /(.*\.map)
mime_type:application / octet-stream
static_files:static / \ 1
上传:static /(.* \.map)

- url:(。*)/
static_files:static / index.html
上传:static

- url:(。*)
static_files:static / index.html
upload:static


I am trying to make an Angular 2 app running on App Engine Standard Environment. It works with the following app.yaml configuration when navigating within the app:

handlers:
- url: /api/.*
  script: _go_app

- url: (.*)/
  static_files: static\1/index.html
  upload: static

- url: (.*)
  static_files: static\1
  upload: static

I can click on a link from / to /clients or /clients/234234 and it works fine.

However if I refresh the browser in a non base path e.g. http://myapp.appspot.com/clients/234234 then I get a 404 error. I guess I need to serve my index.html from all paths which is what I thought (.*)/ and (.*) would do.

How can I set up my handlers/app so I can use HTML5 routing and not let this happen?

解决方案

I have a bunch of static files that need to be served so I added their mappings first. I also (most importantly) changed the way index.html was served:

handlers:
- url: /api/.*
  script: _go_app

- url: /(.*\.svg)
  static_files: static/\1
  upload: static/(.*\.svg)

- url: /(.*\.js)
  static_files: static/\1
  upload: static/(.*\.js)

- url: /(.*\.map)
  mime_type: application/octet-stream
  static_files: static/\1
  upload: static/(.*\.map)

- url: (.*)/
  static_files: static/index.html
  upload: static

- url: (.*)
  static_files: static/index.html
  upload: static

这篇关于如何使页面刷新时使用App Engine的Angular 2路由工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆