2

我尝试了三种方法。

第一个:index.html

<base href="/customer">

第二:app.module.ts

@NgModule({
  providers: [{provide: APP_BASE_HREF, useValue: '/customer'}]
})

第三:app-routing.module.ts

const routes: Routes = [
  { path: "", redirectTo: "/customer", pathMatch: "full" },
  {
    path: "customer",
    component: CustomerComponent,
    data: { animation: "HomePage" }
  }
];

以上所有方法都适用于 URL 路由,我得到了所需的 URL。

http://localhost:4200/客户

但是,静态文件(js 和图像)仍在使用基本路径“ http://localhost:4200/ ”加载。我需要它像http://localhost:4200/customer/main.js 一样

因此,出于某些安全验证原因,我尝试将其设为http://localhost:4200/customer/main.js而不是http://localhost:4200/main.js 。

在下面的屏幕截图中可以看到相同的内容。

在此处输入图像描述

4

3 回答 3

7

您可以使用--baseHref命令行标志,ng serveng build意味着您不再需要为路由添加前缀app-routing.module.ts

ng serve --baseHref=/customer/

建立与

ng build --baseHref=/customer/
于 2019-06-12T08:43:31.717 回答
0

当我从 angular 5 迁移到 7 时,我遇到了完全相同的错误。这可能与 angular 附加构建 js 文件的方式有关。如果您已经添加<base href="/">到 index.html 但脚本仍在从相对路径加载,可能是因为没有<body>标签。尝试将<body>标签添加到 index.html 文件中,看看它是否有效。

于 2019-09-09T06:26:17.843 回答
0

您可以在命令中使用--deploy-url参数。ng build

例子:ng build --deploy-url /my/assets

有关https://angular.io/guide/deployment的更多详细信息。

于 2021-07-09T11:20:13.810 回答