简体   繁体   English

动态配置Angular资产网址

[英]Configure Angular asset url dynamically

Hi i am using angular 7 and i have problem with the production build of the application. 嗨,我使用角度7,我有应用程序的生产版本的问题。 My application lies in a subfolder in the server namely www.hshsh.com/ cm/ where cm is my subfolder where the built files are kept. 我的应用程序位于服务器的子文件夹中,即www.hshsh.com/cm /其中cm是我的子文件夹,其中保存了构建的文件。 I have set the base href as 我已将基础href设置为

--base-href = /cm/

Everything works fine when it comes to downloading the js files and routing. 在下载js文件和路由时,一切正常。 But i have a problem with displaying the images from asset folder as 但我有一个问题,从资产文件夹显示图像为

<img src="../../assets/images/delete_icon.png" width="15px" height="15px" >

It shown 404 error. 它显示404错误。

What i have tried 我试过的

I have tried all below posibilities 我已尝试过以下所有可能性

<img src="../../assets/images/delete_icon.png" width="15px" height="15px" >
<img src="/assets/images/delete_icon.png" width="15px" height="15px" >
<img src="assets/images/delete_icon.png" width="15px" height="15px" >

Also i have tried the css way of doing it 我也尝试过这样做的CSS方式

.class1 {
    background: url("/assets/images/delete_icon.png" width="15px" height="15px""); // Doesnt work.404
}

.class2 {
    background: url("assets/images/delete_icon.png" width="15px" height="15px""); // Compiler error
}

.class3 {
    background: url("/../../assets/images/delete_icon.png" width="15px" height="15px"" width="15px" height="15px""); // 404
}

None of the methods work. 这些方法都不起作用。 How can i configure the asset path without appending the baseUrl manually as 如何配置资产路径而不手动附加baseUrl

Base+asset+imagePath

Please help. 请帮忙。 I am stuck. 我被卡住了。

You should be using ./assets/ for the path. 您应该使用./assets/作为路径。

Also you need to make sure that assets is mentioned in angular.json under build --> options : 此外,您需要确保在angular.json build --> options下的angular.json提到了资产:

"assets": [
              "src/assets"
            ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM