首页 > 解决方案 > 如何在 Angular 7 中设置静态文件的基本路径?

问题描述

我尝试了三种方法。

第一个: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 。

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

在此处输入图像描述

标签: angularangular7angular-routingangular7-router

解决方案


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

ng serve --baseHref=/customer/

建立与

ng build --baseHref=/customer/

推荐阅读