How to change Angular CLI favicon

后端 未结 30 1485
谎友^
谎友^ 2020-12-07 16:32

How can I change the default favicon that is set by the Angular CLI?

I have tried many things, but it always shows the Angular logo as the favicon, even though I hav

相关标签:
30条回答
  • 2020-12-07 16:58

    For those needing a dynamically added favicon here is what I did with an app initializer:

    import { APP_INITIALIZER, FactoryProvider } from '@angular/core'
    
    export function faviconInitFactory () {
    
     return () => {
      const link: any = document.querySelector(`link[rel*='icon']`) || document.createElement('link')
      link.type = 'image/x-icon'
      link.rel = 'shortcut icon'
    
      if (someExpression) {
        link.href = 'url' || 'base64'
      } else {
        link.href = 'url' || 'base64'
      }
    
      document.getElementsByTagName('head')[ 0 ].appendChild(link)
    }
    
    }
    
    export const FAVICON_INIT_PROVIDER: FactoryProvider = {
      provide: APP_INITIALIZER,
      multi: true,
      useFactory: faviconInitFactory,
      deps: []
    }
    

    Just remove fav.ico file under src/ and add this. The favicon will be added before app start

    0 讨论(0)
  • 2020-12-07 16:58

    as simple and easy as :

    1. add your icon or png in the same directory as favicon
    2. edit .angular-cli.json, in assets remove favicon.ico put yours in place
    3. edit index.html, search favicon and put yours in place
    4. run ng serve again

    that's done

    0 讨论(0)
  • 2020-12-07 16:58

    I had the same issue, and solved it by forcing the refreshby the method described here:

    To refresh your site's favicon you can force browsers to download a new version using the link tag and a querystring on your filename. This is especially helpful in production environments to make sure your users get the update.

    <link rel="icon" href="http://www.yoursite.com/favicon.ico?v=2" />
    
    0 讨论(0)
  • 2020-12-07 17:00

    Navigating to the file finally fixed this for me. In my case: http://localhost:4200/favicon.ico

    I had tried to refresh, stop and start ng serve again, and "Empty Cache and Hard Reload", none worked.

    0 讨论(0)
  • 2020-12-07 17:00
    <link rel="icon" type="image/x-icon" href="#">
    

    Add source of your icon and restart app it will change.

    0 讨论(0)
  • 2020-12-07 17:00

    Ok, here in 2020 on 9.1.12. I don't understand why exactly this process is so difficult. I followed almost every post above and none of them worked for me.

    What DID work was this: Totally removing the favicon reference in index.html. It's totally counter intuitive but it works. You DON'T need to put it in the assets folder. I tried all of that but unfortunately none of those suggestions worked.

    index.html

    <!-- <link rel="icon" type="image/x-icon" href="favicon.ico"> DELETE THIS -->
    

    angular.json

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

    and when I run ng build --prod, the favicon is there. Displays on my live server too.

    0 讨论(0)
提交回复
热议问题