简体   繁体   English

在 Azure blob 存储上托管时如何使 Angular 路由工作

[英]How to get Angular routing working when hosting on Azure blob storage

I have created an Angular application which uses lazy loading and the standard angular routing set up and this set up works well when I run the application locally.我创建了一个 Angular 应用程序,它使用延迟加载和标准 angular 路由设置,当我在本地运行该应用程序时,此设置运行良好。 But when I try to serve it from azure blob storage I get a 404 when I navigate around the application.但是当我尝试从 azure blob 存储提供服务时,我在应用程序中导航时收到 404。

Azure storage static site is enabled and when I navigate to the root page, it is displayed. Azure 存储static site已启用,当我导航到根页面时,它会显示。

What Azure configurations do I need to implement to resolve this issue?我需要实施哪些 Azure 配置才能解决此问题?

Please note: In an attempt to keep this question concise as possible I have added my attempts below the code.请注意:为了让这个问题尽可能简洁,我在代码下方添加了我的尝试。 Also I was not sure which Azure configs would be suitable to add so I have left them out until requested.此外,我不确定哪些 Azure 配置适合添加,所以我将它们保留在请求之前。

app-routing.module.ts应用程序路由.module.ts

    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    
    const routes: Routes = [
      { path: '', loadChildren: () => import('./feature-modules/home/home.module').then(h => h.HomeModule) },
      { path: 'roadmap', loadChildren: () => import('./feature-modules/roadmap/roadmap.module').then(h => h.RoadmapModule) },
      { path: 'games', loadChildren: () => import('./feature-modules/games/games.module').then(h => h.GamesModule) },
      { path: 'locations', loadChildren: () => import('./feature-modules/locations/locations.module').then(h => h.LocationsModule) },
      { path: 'tasks', loadChildren: () => import('./feature-modules/site-tasks/site-tasks.module').then(h => h.SiteTasksModule) },
      { path: 'contact', loadChildren: () => import('./feature-modules/contact/contact.module').then(h => h.ContactModule) },
      { path: '**', loadChildren: () => import('./feature-modules/not-found/not-found.module').then(h => h.NotFoundModule) },
    ];
    
    @NgModule({
      imports: [
        RouterModule.forRoot(routes)
      ],
      exports: [RouterModule]
    })
    export class AppRoutingModule { }

Roadmap-routing.module.ts路线图-routing.module.ts

    import { NgModule } from '@angular/core';
    import { RouterModule, Routes } from '@angular/router';
    import { RoadmapComponent } from './roadmap/roadmap.component';
    
    const routes: Routes = [
      { path: '', component: RoadmapComponent }
    ];
    
    @NgModule({
      imports: [RouterModule.forChild(routes)],
      exports: [RouterModule]
    })
    export class RoadmapRoutingModule { }

What I have tried我试过的

ATTEMPT 1尝试 1

Set the error document path to index.html as suggested here and it allows the pages to be serve but still returns a 404 when you look in developer tools.按照此处的建议将错误文档路径设置为 index.html,它允许提供页面,但在您查看开发人员工具时仍会返回 404。 This happens because the page refreshes the requested page cannot be found but the angular routing takes place and finds the requested page.发生这种情况是因为页面刷新找不到请求的页面,但是angular路由发生并找到了请求的页面。 Having a 404 is far from ideal so I continued looking拥有 404 远非理想,所以我继续寻找

ATTEMPT 2尝试 2

I saw this suggestion and altered my app-routing.ts as shown below我看到了这个建议并更改了我的app-routing.ts如下所示

    imports: [RouterModule.forChild(routes, { useHash: true})],

ATTEMPT 3尝试 3

I found this solution but it didn't really apply to my situation as they serve their application through a CDN, which is not what I need at this time我找到了这个解决方案,但它并不真正适用于我的情况,因为他们通过 CDN 提供他们的应用程序,这不是我现在需要的

ATTEMPT 4尝试 4

This suggestion did not work for me.这个建议对我不起作用。 I tried creating a routes.json then found that is deprecated and added staticwebapp.config.json neither worked for me我尝试创建一个routes.json然后发现它已被弃用并添加了staticwebapp.config.json都不适合我

Any help or suggestions will be greatly received任何帮助或建议都会收到

You cannot use Azure Blob Storage with Static Site feature if you use Angular routing / SPA as it is not designed for that.如果您使用 Angular 路由/SPA,则不能将 Azure Blob 存储与 Static 站点功能一起使用,因为它不是为此设计的。 So that's the answer to your question.这就是你问题的答案。

... And that's why Microsoft released Azure Static Web App ... 这就是微软发布 Azure Static Web App 的原因

Regarding your attempt #4, which is the right approach (so, not related to your initial question).关于您的尝试 #4,这是正确的方法(因此,与您最初的问题无关)。 So you should post another question related to Azure Static Web App and provide some details of what you have tried to work with Angular routing.因此,您应该发布另一个与 Azure Static Web 应用程序相关的问题,并提供您尝试使用 Angular 路由的一些详细信息。

I tried with "staticwebapp.config.json" and followed Microsoft documentations, I didn't get any issue for my Angular SPA application我尝试使用“staticwebapp.config.json”并遵循 Microsoft 文档,我的 Angular SPA 应用程序没有遇到任何问题

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

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