angular - 如何在 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。
但是,静态文件(js 和图像)仍在使用基本路径“ http://localhost:4200/ ”加载。我需要它像http://localhost:4200/customer/main.js 一样。
因此,出于某些安全验证原因,我尝试将其设为http://localhost:4200/customer/main.js而不是http://localhost:4200/main.js 。
在下面的屏幕截图中可以看到相同的内容。
解决方案
您可以使用--baseHref
命令行标志,ng serve
这ng build
意味着您不再需要为路由添加前缀app-routing.module.ts
ng serve --baseHref=/customer/
建立与
ng build --baseHref=/customer/
推荐阅读
- javascript - 如何从 TinyMCE 获取页数?
- python - 从导入的模块中打印当前运行的 python 脚本的 docString
- ionic-framework - 带电容器的 Ionic v5 网络媒体捕获 - cordova_not_available
- python - 如何在 AWS ec2 中安装 google-ads (grpcio)
- javascript - 通过socket io进行屏幕共享
- python - 带注释的热图轴顺序取决于数据类型
- python - 在 PySimpleGUI 中更改输入框的背景颜色
- asp-classic - 是否可以注入 Cookie、Form、Session 数据?
- python - 如何访问 python 字典键作为 pydantic 模型字段
- oracle - 我是否需要为 SSL 配置 SQL*Plus?