Serve assets as they are dynamically added

2019-06-11 21:05发布

问题:

I am writing an Angular application that runs along a NodeJs app. The NodeJs app watches a folder called gallery inside the Angular app folder. The idea is that when an image is added to the gallery folder, it should be added to an image slideshow running as part of the Angular app.

Because the app is served in memory, when the image is added to the slideshow, it 404s. I have to kill ng serve and restart it in order for the image to load. I'd like to see the image actually load into the slideshow without me having to kill ng serve and restart it.

Is there a workaround to this?

The gallery folder is part of the assets folder in the cli config and it does get copied over. It's only when adding files to this folder while the ng serve is running that the image fails to load.

Thanks!

回答1:

Open angular-cli.json and edit the assets properties of your app.

 "assets": [
    "assets",
    "favicon.ico",
    { "glob": "**/*", "input": "./assets/gallery", "output": "./assets/gallery" }
  ],

Webpack will still need to recompile these assets, but at least you don't have to start and stop the app.

Angular CLI Documentation