我是 Angular 4 的新手;我已经使用 npm install -g @angular/cli
安装了.然后使用 ng new my-app
创建了一个新元素.
|-src
| -app
| app.component.css
| app.component.html
| app.component.spec.ts
| app.component.ts
| app.module.ts
src
文件夹包含 app 文件夹,其中包含相应的上述 css、ts、html 文件。现在我在应用程序文件夹中添加了一个图像
|-src
| -app
| app.component.css
| app.component.html
| app.component.spec.ts
| app.component.ts
| app.module.ts
| image.jpg
问题是,当我添加 <img src="image.jpg">
app.component.html 中的标签,它显示一个 404
控制台中的错误。但是,如果我添加类似 background: url('image.jpg');
的样式在 app.component.css 中,它正在工作。
有什么建议吗?谢谢。
最佳答案
您应该将所有公共(public) Assets (例如图像)放在 src/assets
文件夹中
然后你可以像这样通过你的html/css
访问它们/assets/image.jpg
文件结构应该如下:
- src
- app
- assets
image.jpg
关于javascript - 本地文件在 angularjs 4 中显示 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44238600/