2017-05-29 20 views
0

我是新角度4;我已經安裝使用npm install -g @angular/cli。然後使用ng new my-app創建了一個新項目。本地文件在angularjs中顯示404錯誤4

|-src 
| -app 
| app.component.css 
| app.component.html 
| app.component.spec.ts 
| app.component.ts 
| app.module.ts 

src文件夾包含app文件夾,其中包含相應的上述css,ts,html文件。現在,我已經在app文件夾

|-src 
| -app 
| app.component.css 
| app.component.html 
| app.component.spec.ts 
| app.component.ts 
| app.module.ts 
| image.jpg 

的問題是增加了一個圖像,當我在app.component.html添加<img src="image.jpg">標籤,它顯示在控制檯中404錯誤。但是如果我在app.component.css中添加一個類似background: url('image.jpg');的樣式,它就可以工作。

有什麼建議嗎?謝謝。

+0

給圖像路徑位置 – jose

回答

1

你應該把所有的公共資產(如圖像)在src/assets文件夾

然後你就可以通過你的html/css訪問他們這樣/assets/image.jpg

文件結構應如下:

- src 
    - app 
    - assets 
     image.jpg 
+0

謝謝你的工作,首先爲什麼我們需要這樣做?任何添加到資產的東西都可以像.json文件那樣訪問? – Sam

+0

關鍵是將您的應用程序代碼文件(ts,html和css)與公共資產文件分開,這樣瀏覽器就可以訪問這些圖像。所以是的,你資產文件夾中的所有東西都可以通過'my-domain.com/assets/some_file'公開訪問 –

0

您需要給圖像路徑位置。你可以這樣做:

<img src="./image.jpg"> 

這樣你可以給你的image.jpg文件的參考。