Angular+VS Code开发中图片无法显示的问题求助
Angular项目assets资源未打包导致图片无法加载问题
问题详情
- 学习Angular过程中,按教程要求在header组件中添加图片引用,尝试过双引号嵌套单引号的写法,无效;图片确实存在于assets文件夹内
- 将app.component.html内容替换为
<app-header/> - 构建启动后访问
http://localhost:4200,页面正常显示但图片无法加载,且assets文件夹未出现在dist目录中 - 两种图片路径写法均无效:
<img src="assets/..."/>和<img src="/assets/..."/>
相关截图
VS Code工作区

Chrome浏览器显示效果

Chrome页面源码
<!DOCTYPE html><html lang="en"><head> <script type="module" src="/@vite/client"></script> <meta charset="utf-8"> <title>MyFirstProject</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"><link rel="stylesheet" href="styles.css"><style ng-app-id="ng">header[_ngcontent-ng-c4022922458] { background-color: lightgray; box-shadow: 8px 8px 8px rgba(0, 0, 0, 0.5);}/*# sourceMappingURL=header.component.css.map */</style></head><body><!--nghm--> <app-root _nghost-ng-c3361530648="" ng-version="18.0.3" ngh="0" ng-server-context="ssr"><app-header _ngcontent-ng-c3361530648="" _nghost-ng-c4022922458="" ngh="0"><header _ngcontent-ng-c4022922458=""><img _ngcontent-ng-c4022922458="" src="/assets/Logoduitbluesmall.jpg" alt="bla"></header></app-header><br _ngcontent-ng-c3361530648="">xxx <img _ngcontent-ng-c3361530648="" src="/assets/Logoduitbluesmall.jpg" alt="uff"><br _ngcontent-ng-c3361530648="">yyy <img _ngcontent-ng-c3361530648="" src="/assets/Logoduitbluesmall.jpg" alt="zack"></app-root><script src="polyfills.js" type="module"></script><script src="main.js" type="module"></script><script id="ng-state" type="application/json">{"__nghData__":[{}]}</script></body></html>
环境信息
- Visual Studio Code版本:1.91.0(用户安装版)
- Commit: ea1445cc7016315d0f5728f8e8b12a45dc0a7286
- Date: 2024-07-01T18:52:22.949Z
- Electron: 29.4.0
- ElectronBuildId: 9728852
- Chromium: 122.0.6261.156
- Node.js: 20.9.0
- V8: 12.2.281.27-electron.0
- OS: Windows_NT x64 10.0.19045
- 运行方式:Windows10电脑的WSL控制台中启动VS Code
内容的提问来源于stack exchange,提问作者du-it
相关产品推荐
相关产品推荐

