javascript - 重新加载页面时,生产 URL 中的 Angular 2 会被破坏

标签 javascript angular typescript build angular-routing

当我加载应用程序并重定向到另一个路由时,我正在使用 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/

相关文章:

javascript - 检测数组项中的 Mailto 与 Http/Https 并在新选项卡中打开 Http url

Angular 2动态标签加载器

Typescript:以编程方式访问 VS Code 的 "Find All References"

angular - 函数从 pouchDB 获取委托(delegate),插入一些调试数据并返回一个 promise

reactjs - 在使用带有钩子(Hook)的导出类时,我应该远离类吗?

javascript - Jquery点击函数无法识别

javascript - 订阅 Observable 不触发变更检测

javascript - 外循环和内循环中的 ng-repeat 和 $index

html - 在 Angular2 中显示默认图像

javascript - 对嵌套数组 javascript 中的对象列表进行分组