当网站托管在子域中时,如何将图像 src 放置在 angular-cli prod build 的 dist 中?
how to place image src in dist on angular-cli prod build, when website is hosted in a subdomain?
我有设置和 angular-cli 产品构建。图片放在assets/images/somelogo.png.
我在 IIS 上有一个网站:http://localhost:8000/
我创建了一个 sub-website 并试图在其中托管 angular-cli 构建。其中url如下:http://localhost:8000/a2
我按如下方式创建构建:
ng build --prod --deploy-url /a2
我有一个 header 组件,它有一个 html 如下:
<a class="navbar-brand" href="/">
<img src="/assets/images/somelogo.png" alt="somelogo" />my image</a>
但在部署时图像 url 显示为:/assets/images/somelogo.png
但我希望它显示:a2/assets/images/somelogo.png
我还尝试将 base-href 更改为 运行 以下内容:
ng build --prod --deploy-url /a2 --base-href /a2
但这也不管用。有人可以帮我吗?
============================================= ============================
我通过执行以下操作修复了它:
在我使用的模板上:
<img [src]="imageSource"/>
在模板组件上我做了以下操作:
import {PlatformLocation } from '@angular/common';
export class MyComponent {
constructor(private platformLocation: PlatformLocation) {
imageSource:string=this.platformLocation.pathname +'assets/images/somelogo.png';
}
platformLocation.pathname 获取 base-href 并将其附加到路径中。我不确定这是否是正确的解决方案?
我试过了
ng build --prod --base-href .
成功了,感谢 Will Huang 的回复
我有设置和 angular-cli 产品构建。图片放在assets/images/somelogo.png.
我在 IIS 上有一个网站:http://localhost:8000/ 我创建了一个 sub-website 并试图在其中托管 angular-cli 构建。其中url如下:http://localhost:8000/a2
我按如下方式创建构建:
ng build --prod --deploy-url /a2
我有一个 header 组件,它有一个 html 如下:
<a class="navbar-brand" href="/">
<img src="/assets/images/somelogo.png" alt="somelogo" />my image</a>
但在部署时图像 url 显示为:/assets/images/somelogo.png 但我希望它显示:a2/assets/images/somelogo.png
我还尝试将 base-href 更改为 运行 以下内容:
ng build --prod --deploy-url /a2 --base-href /a2
但这也不管用。有人可以帮我吗?
============================================= ============================
我通过执行以下操作修复了它:
在我使用的模板上:
<img [src]="imageSource"/>
在模板组件上我做了以下操作:
import {PlatformLocation } from '@angular/common';
export class MyComponent {
constructor(private platformLocation: PlatformLocation) {
imageSource:string=this.platformLocation.pathname +'assets/images/somelogo.png';
}
platformLocation.pathname 获取 base-href 并将其附加到路径中。我不确定这是否是正确的解决方案?
我试过了
ng build --prod --base-href .
成功了,感谢 Will Huang 的回复