1

我正在尝试使用单水疗库将我的应用程序实现为微前端。应用程序的文件夹结构如下,

  • 微前端
    • 根应用
    • 导航栏应用程序(Angular 应用程序)
    • 仪表板应用程序(Angular 应用程序)
    • 服务应用程序(Angular 应用程序)

我已经在服务器中部署了相同的内容,并且网站按预期工作。服务器中的文件夹结构(/var/www/html/)如下,

  • 家(根)[网址:https://microfrontendapp.com/home]
  • 仪表板 [网址:https://microfrontendapp.com/dashboard/my-dashboard]
  • 服务 [网址:https://microfrontendapp.com/service]
  • 导航栏

我面临的问题是我从主页开始使用该网站,然后移至仪表板或服务应用程序,如果我尝试从此 URL 路径重新加载,我会收到404 Page not found error for the web URL

我正在尝试使用路由散列来解决问题。代码如下:

主页(根)应用程序

索引.html

 <template id="single-spa-layout">
    <single-spa-router mode="hash" base="/">
      <div style="display: flex">
        <nav>
          <application name="navbar"></application>
        </nav>
        <div style="width: 96%; margin: 5% 1%">
          <route path="/dashboard">
            <application name="dashboard"></application>
          </route>
          <route path="/service">
            <application name="service"></application>
          </route>
        </div>
      </div>
    </single-spa-router>
  </template>

根应用程序.js

System.import("single-spa").then(function(singleSpa) {
  singleSpa.registerApplication(
    "navbar",
    function() {
      return System.import("navbar");
    },
    function(location) {
      return true;
    }
  );

  singleSpa.registerApplication(
    "dashboard",
    () => System.import("dashboard"),
    location => location.pathname.startsWith("/dashboard"),
    { some: "value" }
  );

 singleSpa.registerApplication(
    "service",
    () => System.import("service"),
    location => location.pathname.startsWith("/service"),
    { some: "value" }
  );

导航栏应用

onNavigation() {
        window.location.pathname += "/"; 
// The above was used because on during navigation to dashboard or any other app, URL is changed as https://microfrontendapp.com/home#/dashboard/my-dashboard
        window.location.hash = "/" + url; // To point to the selected navigation URL
}

仪表板应用程序

路由器.ts

const routes: Routes = [
    {
        path: 'dashboard',
        component: DashboardComponent,
        children: [
            {
                path: 'my-dashboard',
                component: MyDashboardComponent,
                data: { }
            }
      }
]
@NgModule({
    imports: [RouterModule.forRoot(routes), { useHash: true, preloadingStrategy: NoPreloading }],
    exports: [RouterModule],
    providers: [
        { provide: APP_BASE_HREF, useValue: '/' },
    ],
})
export class AppRoutingModule { }

本地服务器 URL 行为如下,

  1. 主页:http://localhost:4200/#/
  2. 导航到仪表板:http://localhost:4200/#/dashboard/my-dashboard
  3. 在上面的 URL 上重新加载时,它工作得很好。

当部署在 apache 服务器中时,行为如下,

  1. 主页:https ://microfrontendapp.com/home
  2. 导航到仪表板:https ://microfrontend.com/home/#/dashboard/my-dashboard 。但是页面没有加载。没有 HTTP 调用来获取主脚本文件(仪表板应用程序编译的脚本文件)来加载页面。

有人可以帮助解释为什么脚本文件的 HTTP 调用没有启动吗?我不确定我是否做错了什么。或者有没有其他解决方案可以解决404 Page not found 问题

请在这里帮助我。提前致谢。

4

2 回答 2

0

你的路线有些不规则。

早些时候,当您使用 时PathLocationStrategy,您的 URL 看起来像: https ://microfrontendapp.com/dashboard/my-dashboard

之后HashLocationStrategy,您的 URL 就像: https ://microfrontend.com/home/#/dashboard/my-dashboard

这是什么 /home/ 这里?如果这是上下文路径,您可能必须考虑baseHref在您的应用程序中添加。

在这里查看我的答案

于 2021-10-27T08:15:32.530 回答
0

这是您的部署服务器的问题,而不是单水疗中心或任何客户端的问题,真的。Apache 需要配置为使用相同的 index.html 来处理每个请求,这对于任何单页应用程序,即使不使用 single-spa 也必须这样做。

对此没有单一的解决方案,因为您可能需要以不同方式处理特定路由,但AngularVue为如何在各种服务器上进行配置提供了很好的起点。对于 Apache,它可能看起来像这样:

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
于 2021-10-25T15:16:49.513 回答