How to inject SVG icon sprites in Angular component HTML DOM?

前端 未结 4 2215
失恋的感觉
失恋的感觉 2021-02-15 22:38

I am building an Angular application (Angular 4/5/6) and would like to use SVG sprites in my component template.

Question: Assuming I already have my S

4条回答
  •  执念已碎
    2021-02-15 22:48

    I think your root path is where you are having problems. In your code you are telling angular to look in app but the browser sees this as https://your-site.com./icons

    If you move your icon file under your /assets folder, then it should be available already because the src\assets folder is already included in angular.json, the "assets" collection tells Angular where to look.

    
        // Notice root path not "./"
    
    

    If you would like your files to be served from another directory all you need to do is add a path in your angular.json:

    …
    "assets": [
       "src/favicon.ico",
       "src/assets",
       "src/your-dir"
    ],
    …
    

    Then in your code

    
       
    
    

    I wouldn't suggest adding /src/app as an asset path, this would basically open up your entire app for serving files, making your entire directory accessible.

    I forked your example and updated here

提交回复
热议问题