You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Pages从docs目录部署后图片无法加载问题求助

解决GitHub Pages部署Angular项目图片无法加载的问题

核心问题:图片引用路径不匹配部署结构

你的base href已设置为"/SindhuApp/",且通过/docs目录部署,这意味着GitHub Pages会把/docs下的内容作为https://<你的用户名>.github.io/SindhuApp/的根目录。但你当前的图片引用路径(src/assets/xxx、docs/assets/xxx)完全不符合部署后的实际文件结构,导致图片无法被找到。

具体修正步骤

  1. 修正图片引用路径
    直接使用基于base href的绝对路径,或者利用Angular的base特性使用相对路径:

    <div>
    <p class="Anime">
        <img src="/SindhuApp/assets/angular5.png" alt="Angular Image" style="height: 70px; width:80px;">
        <img src="/SindhuApp/assets/ts.png" alt="TS Image" style="height: 70px; width:70px"> 
        <img src="/SindhuApp/assets/html.png" alt="HTML Image" style="height: 75px; width:75px">
        <img src="/SindhuApp/assets/css.png" alt="CSS Image" style="height: 75px; width:75px">
        <img src="/SindhuApp/assets/material.png" alt="Material Image" style="height: 65px; width:70px">
    </p>
    </div>
    

    相对路径写法(需确保Angular编译后路径正确):

    <img src="./assets/angular5.png" alt="Angular Image" style="height: 70px; width:80px;">
    
  2. 确认Angular的资源配置
    打开项目根目录的angular.json,检查architect.build.options.assets,确保src/assets被配置为编译后复制到dist目录的assets文件夹:

    "assets": [
      "src/favicon.ico",
      "src/assets"
    ]
    
  3. 重新编译部署
    执行编译命令时指定base-href参数,保证编译后的路径匹配GitHub Pages部署路径:

    ng build --prod --base-href "/SindhuApp/"
    

    把dist目录下的所有文件复制到项目的/docs目录,提交到GitHub即可。

验证方式

部署完成后,直接在浏览器访问图片完整URL(比如https://<你的用户名>.github.io/SindhuApp/assets/angular5.png),如果能正常显示,说明路径配置正确。

内容的提问来源于stack exchange,提问作者Surya Vasireddy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 23:07:27