当我加载应用程序并重定向到另一个路由时,我正在使用 Nginx 服务器在生产中运行我的应用程序,它工作正常,但当我重新加载页面时,我收到 404 Not Found 错误。
考虑一下,如果我在 www.example.com
加载应用程序,那么在重新加载页面后路线也可以正常工作。
如果我重定向到另一条路线www.example.com/about
,则页面首先会加载,但如果我以相同的状态重新加载页面,则会收到 404 Not Found 错误。
我找到了同样的解决方案
import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {LocationStrategy, HashLocationStrategy} from '@angular/common';
import {MyApp} from './myapp';
bootstrap(MyApp, [
ROUTER_PROVIDERS,
{provide: LocationStrategy, useClass: HashLocationStrategy}
]);
上面的解决方案工作正常,但问题是我得到带有 # 的 URL
www.example.com/#
www.example.com/#/about
但我的要求是我不想在 URL 中包含 #
即使我按照下面给出的方式对 Nginx .config 文件进行了更改
location /subfolder/myapp/ {
try_files $uri /subfolder/myapp/index.html;
}
但 URL 仍然出现相同的错误
请提供任何帮助,谢谢
最佳答案
试试这个...
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
# Location of asset folder
location ~ ^/(assets)/ {
gzip_static on;
gzip_types image/svg+xml text/plain text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
expires 0;
}
关于javascript - 重新加载页面时,生产 URL 中的 Angular 2 会被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44455774/