38

I'm currently working on a small Angular2 project. In my case i have to reroute some of the requests (like "/faq", "/aboutus") back to my old backend server to get some server side rendered thymeleaf templates. Therefore i'm using the built in proxy to reroute to my backende server. Sadly for some weird reason it only servers the html files without any scripts and styles or images. (i used the angular-cli to create my project structure)

Thats why i wanted to add these static files into my angular2 folder but i can't find the correct place to make it available to my application. Does anyone else know how to correctly place these file inside the project structure ?

Thanks in advance for any help

2 Answers 2

50

According to the current angular-cli readme (v1.0.1):

You use the assets array in angular-cli.json to list files or folders you want to copy as-is when building your project:

"assets": [
  "assets",
  "favicon.ico"
]

By default the assets folder is configured for this, so you can place your files into a structure like

├── src
.   ├── assets
.   .   ├── file1.txt
.   .   ├── img
    .   │   └── image1.png
        └── css

and serve them from url path /img/image1.png etc.

If you're not happy with the default option, add a folder name of your choice to angular-cli.json, i.e.

"assets": [
  "static",
  ...
]

Create the ./src/static/ folder for your files and serve analogously to the default.

Sign up to request clarification or add additional context in comments.

6 Comments

I got this error: Cannot match any routes. URL Segment: 'assets/images/test.png'. anything missing? Thanks.
@Rocky Don't serve it from "/assets/images/test.png", serve it from "/images/test.png"
FYI: for angular 4, you have to serve them with assets/img/image1.png etc
how do i serve html page?
I had to ../assets/logo.png for angular-7
|
0

It's now the public/ folder:

Contains image and other asset files to be served as static files by the dev server and copied as-is when you build your application.

https://angular.dev/reference/configs/file-structure

angular.json should contain:

"assets": [
  {
    "glob": "**/*",
    "input": "public"
  }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.