Angular-cli: How can I access images outside of the assets folder

前端 未结 2 1819
情话喂你
情话喂你 2021-01-02 09:29

I\'m very new to Angular and building a photo lightbox as a learning project. Is there a way to use images from outside the project folder? I\'ve tried using the Glob as ref

2条回答
  •  情书的邮戳
    2021-01-02 10:17

    You should be able to accomplish this with the glob. For example if I have a folder structure

    /outsideDirectory
        testImg.jpg
    /myProject
        /node_modules
        /src
        .angular-cli.json
    

    In your cli.json you can define the glob

    "assets": [
        {"glob": "**/*", "input": "../../outsideDirectory", "output": "./assets/"}
    ]
    

    This will copy the contents of your outsideDirectory to the /dist/assets/ directory. Now you can use the image within your code

    
    

    Note the cli.json's assets is for static assets. If you are pushing images to outsideDirectory dynamically, it is better to host the images yourself or use some service, then reference them with a full Url. This is because the images from your glob are only a copy of the outsideDirectory contents (that occurs at build time).

提交回复
热议问题