简体   繁体   English

使用静态网站回退问题在 Azure 上部署 Angular

[英]Deploy Angular on Azure with static website fallback problem

I'm trying to deploy my Angular app to the Azure static websites.我正在尝试将我的 Angular 应用程序部署到 Azure 静态网站。 I configured the environment following this tutorial: Medium tutorial for Azure static websites我按照本教程配置了环境: Azure 静态网站的中等教程

The problem that I have is the fallback in the case I try to access for example myurl.com/mypage (with only myurl.com is working correctly).我遇到的问题是在我尝试访问例如 myurl.com/mypage(只有 myurl.com 正常工作)的情况下的后备。

I'm building Angular with ng build --prod .我正在使用ng build --prod

As it's running on IIS, I've tried the following web.config: web.config , I put it in /src folder and I've changed angular.json as follows:由于它在 IIS 上运行,我尝试了以下 web.config: web.config ,我将它放在 /src 文件夹中,并且我已将 angular.json 更改为如下:

"assets": [
     ...
     "src/web.config"
],

and it's placing the file in the root folder which is correct.并将文件放在正确的根文件夹中。

My problem is that I'm still getting 404 when I try to access myurl.com/mypage我的问题是当我尝试访问myurl.com/mypage时仍然收到 404

Maybe this can help somebody else:也许这可以帮助其他人:

On your Storage Account -> Static website you will find:在您的存储帐户 -> 静态网站上,您将找到: 在此处输入图片说明

Both should be index.html两者都应该是 index.html

You can specify your fallback route by creating a file called routes.json in src/assets.您可以通过在 src/assets 中创建一个名为 routes.json 的文件来指定回退路由。 It should contain:它应该包含:

{
  "routes": [
    {
      "route": "/*",
      "serve": "/index.html",
      "statusCode": 200
    }
  ]
}

I specified staticwebapp.config.json file with the following contents in the src folder.我在 src 文件夹中指定了具有以下内容的 staticwebapp.config.json 文件。 This worked for me and resolved the deep linking issue on the Angular site.这对我有用并解决了 Angular 站点上的深层链接问题。 I also had "useHash" enabled.我还启用了“useHash”。 Hope this helps someone.希望这可以帮助某人。 I initially tried this fix on the new Azure Static Web App but this rewrite is also working on Azure Storage-Static Web site.我最初在新的 Azure 静态 Web 应用程序上尝试了此修复程序,但此重写也适用于 Azure 存储静态网站。

 {
   "navigationFallback": {
    "rewrite": "index.html",
     "exclude": [ "/images/*.{png,jpg,gif,ico}", "/*.{css,scss,js}" ]
   }
 }

Using the below provider in your app.module.ts should be enough:在 app.module.ts 中使用以下提供程序就足够了:

{provide: LocationStrategy, useClass: HashLocationStrategy}

Example:例子:

...other imports
import { HashLocationStrategy, LocationStrategy } from '@angular/common';

@NgModule({
    declarations: [AppComponent],
    imports: [BrowserModule],
    providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
    bootstrap: [AppComponent],
})
export class AppModule {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM