简体   繁体   中英

Import globally used library module into angular project modules

I think I did something wrong here.

I want to use angular-fontawesome module . Following the instructions to import it into AppModule successfully.

export class AppModule {
constructor(library: FaIconLibrary) {
    // Add an icon to the library for convenient access in other components
    library.addIcons(
        faUsers,
        faUserCircle,
        faDesktop,
        faTachometerAlt,
        faWrench,
        faFolder,
        faChartArea,
        faTable,
        faSearch,
        faBell,
        faFileAlt,
        faExclamationTriangle,
        faDonate,
        faEnvelope,
        faUser,
        faCogs,
        faList,
        faSignOutAlt,
        faAngleUp,
        faLaughWink,
        faServer,
        faWarehouse,
        faPlusCircle
    )
}     

But when I want to use it in my own in-project module, say admin.module.ts . I have to import all that again? The same code have to be placed into admin.module.ts again?

It seems to me. AppModule is the root module. I get that if I have other modules that I want to use icons then I might have to do this. But, really? Even the AppModule is not enough?

The FaIconLibrary is an application-level singleton , so you only need to add icons to the library once. However you should add FontAwesomeModule to imports of your AdminModule to make <fa-icon> component accessible to the components declared in the AdminModule (it's a standard Angular thing ).

You may also find this answer interesting to read.

According to documentation

Icons can be registered once in app.module with FaIconLibrary.addIcons() or FaIconLibrary.addIconPacks(). Icons added to the library will be available to any other components whose parent module also imports FontAwesomeModule.

So re check next steps

1   Import { FontAwesomeModule, FaIconLibrary } from '@fortawesome/angular-fontawesome'
2   Add FontAwesomeModule to imports
3   Inject FaIconLibrary into constructor of the module.
4   Import an icon like { faCoffee } from '@fortawesome/free-solid-svg-icons'
5   Add icon to the library with library.addIcons(faCoffee)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM