通过浏览器中的地址栏在redirect角度应用程序中出现错误404但是在通过ng serve命令运行角度应用程序后,这在浏览器中何时起作用一切正常。 我在Nginx中的配置是:

 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                root “D:\Tools\nginx-1.16.0\nginx-1.16.0\html”;
                try_files $uri $uri/ /index.html?$args;
       }

注意: 目前在索引html文件中定义基数

 <base href="/">
分析解答

你有两个选择,你可以使用任何适合你的选择:

  1. 将Nginx服务器配置规则中的更改更改为redirect。
  2. 使用Angular的HashLocationStrategy

#1

我不知道Nginx的配置​​设置,但是对于IIS。您可以在下面找到一些等价物:

<system.webServer>
 <rewrite>
      <rules>
        <rule name="redirect all requests" stopProcessing="true">
          <match url="^(.*)$" ignoreCase="false"/>
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false"/>
          </conditions>
          <action type="Rewrite" url="samcds/index.html" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

2。

在您的应用模块中,添加以下提供商:

 providers: [Location, {provide: LocationStrategy, useClass: HashLocationStrategy}], 

供参考https://angular.io/api/common/HashLocationStrategy