Skip to content

Instantly share code, notes, and snippets.

@fourgates
Last active July 31, 2023 20:37
Show Gist options
  • Save fourgates/703b0b58c3d9ef1db3e05f5cc2c6308e to your computer and use it in GitHub Desktop.
Save fourgates/703b0b58c3d9ef1db3e05f5cc2c6308e to your computer and use it in GitHub Desktop.
This gist demonstrates how how to use a barrel file to group together angular components in a single import.
// This is a way to get best of both worlds of grouping component together
// into "modules" (barrels) and using standalone components!
//
// using a "barrel" export you can group together angular components so you only need to
// import one object
//
// a good use case for this is components that are dependent on each other or commonly used together
// in this example we have a table and table-sort directive bc you almost always want to be able to
// sort a table!
//
// however, you can also just import the table or the table-sort directive if you wanted to!
//
// FYI -- this angular library gets built with the following cmd: `yarn build-lib && yarn pack-lib`
// 1. export a group of components from an angular library / project / component
@Directive({...standalone: true...})
export class OurTableSortByDirective { .. }
@Component({...standalone: true...})
export class OurTableComponent {...}
// 2. THIS IS THE BARREL!
export const OUR_TABLE = [
OurTableComponent,
OurTableSortByDirective
] as const;
// 3. import the barrel into the client component!
// some-example-component-with-tables.component.ts
import {
OUR_TABLE,
} from 'our-ui-library';
...
@Component({
...
standalone: true,
imports: [
...
OUR_TABLE,
...
],
...
})
export class SearchTableComponent {...}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment