Google Cloud App Engine中的Angular 7路由不起作用 [英] Angular 7 Routing in Google Cloud App Engine not working
问题描述
我已经向Google Cloud App Engine发布了angular 7应用程序.
I've published an angular 7 Application to Google Cloud App Engine.
索引页面正在加载,但是子目录给了我
The index page is loading, but the subdirectorys give me
Error: Not Found
The requested URL /admin was not found on this server.
这是我的app.yaml:
This is my app.yaml:
runtime: nodejs10
env_variables:
environment: "--prod"
handlers:
- url: /
static_files: dist/XXX/index.html
upload: dist/XXX/index.html
- url: /
static_dir: dist/XXX/
- url: /.*
secure: always
script: auto
我终于弄清楚了app.yaml中的路由如何用于Angular应用程序. 这是我的工作app.yaml:
I finally figured out, how the routing in app.yaml works for Angular Applications. Here is my working app.yaml:
runtime: nodejs10
env_variables:
environment: "--prod"
handlers:
- url: /
secure: always
static_files: dist/index.html
upload: dist/.*
- url: /(.*\.js)
secure: always
redirect_http_response_code: 301
static_files: dist/\1
upload: dist/.*\.js
- url: /(.*\.css)
secure: always
redirect_http_response_code: 301
static_files: dist/\1
mime_type: text/css
upload: dist/.*\.css
- url: /.*
secure: always
static_files: dist/index.html
upload: dist/.*
推荐答案
我认为,如果您的资源文件只有js和css,则处理程序中的路由规则可以正常工作.如果您有图像文件,音频文件等,则必须对regex使用更通用的路由规则:
I think your routing rules in handlers work fine if your resource files are only js and css. If you have image files, audio files, etc, you must use a more generic routing rule with regex:
handlers:
- url: /
secure: always
static_files: www/index.html
upload: www/index.html
# Routing rules for resources, css, js, images etc. Any file with format filename.ext
- url: /(.*\.(.+))$
secure: always
static_files: www/\1
upload: www/(.*\.(.+))$
# Routing rule for Angular Routing
- url: /(.*)
secure: always
static_files: www/index.html
upload: www/index.html
想法是相同的,但是从语法上讲,通配符匹配格式为filename.*的任何文件将处理所有资源文件.
The idea is the same, but syntactically, a wild card match for any files with format filename.* will handle all the resource files.
这篇关于Google Cloud App Engine中的Angular 7路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!