3 - Generating components and Nx lib

1. Generate our first Nx lib

  • Run the below command to see all the lib options

nx g lib --help

The output for the current Nx version (9.3.0) is:

>nx g lib --help
nx generate @nrwl/angular:library [name] [options,...]
Options:
  --name                  Library name
  --directory             A directory where the lib is placed
  --publishable           Generate a buildable library.
  --prefix                The prefix to apply to generated selectors.
  --skipFormat            Skip formatting files
  --simpleModuleName      Keep the module name simple (when using --directory)
  --skipPackageJson       Do not add dependencies to package.json.
  --skipTsConfig          Do not update tsconfig.json for development experience.
  --style                 The file extension to be used for style files. (default: css)
  --routing               Add router configuration. See lazy for more information.
  --lazy                  Add RouterModule.forChild when set to true, and a simple array of routes when set to false.
  --parentModule          Update the router configuration of the parent module using loadChildren or children, depending on what `lazy` is set to.
  --tags                  Add tags to the library (used for linting)
  --unitTestRunner        Test runner to use for unit tests (default: jest)
  --dryRun                Runs through and reports activity without writing to disk.
  --help                  Show available options for project target.
  • Add a new lib called auth. We will not lazy load this lib as auth will always be used by our app. Choose SASS as your styelsheet language.

The output should look like this:

  • Inspect the index.ts file which is for exporting public api surface for the lib.

  • Inspect angular.json libs array allow us to use --project flag with libs to get cli scaffolding and code generation.

Currently issue with setting scss as default for lib. When ever you make a lib component please change style extension .scss

2. Add container and presentational components

Lets look at what this pattern is and what are the benefits in slides https://docs.google.com/presentation/d/1xf8aPIvQjgjUVGH_1sRkikvh5H73x2xvX7PnN4AjYt4/edit#slide=id.g3bc936a676_1_4

Characteristics of Container and Presentational Components
  • Add a new container component to the auth lib

The output will look like this:

  • Add a new presentational component to the auth lib

Note: You may decide not to make this a presentational component but it makes it easier to test and refactor.

As this is a child route it is assumed that the consuming module will already prefix the route with "localhost:4200/auth" and when we say the path is "login" we mean relative to this so it will end up being "localhost:4200/auth/login".

3. Add a default route to the auth module

4. Update the consuming Customer Portal App module

  • Delete everything but the router-outlet on the apps app.component.html file.

  • Add the Auth module to the main App Module

App running in the Browser

5. Add presentational component to container component

  • Add the presentational component to the container component.

  • Add login function to container component class

6. Add new folder for shared interfaces

  • Generate a new library using the nx cli.

  • Add a 'authenticate.d.ts' file to the lib folder and export the added data models from the data-models.module.ts file

  • Export the interface

  • Add basic login form to presentational component

  • Add an angular @Output to emit the event of a form submission

7. Change the ChangeDetectionStrategy to OnPush

  • Now that we are using the presentation and container component pattern and we know that we only need to check the child components for changes if a DOM event or a @Input or @Output passes new primitives or reference values. In this way we can tell Angular not check the whole component tree which can cause performance issues in larger applications.

Last updated