Optional dependencies
Optional dependencies in Angular are really powerful when you use or configure a dependency that may or may not exist or that has been provided within an Angular application. In this recipe, we'll learn how to use the @Optional
decorator to configure optional dependencies in our components/services. We'll work with LoggerService
and ensure our components do not break if it has not already been provided.
Getting ready
The project for this recipe resides in chapter03/start_here/ng-optional-dependencies
. Perform the following steps:
- Open the project in Visual Studio Code.
- Open the Terminal, and run
npm install
to install the dependencies of the project. - Once done, run
ng serve -o
.This should open the app in a new browser tab. You should see something similar to the following screenshot:
Now that we have the app running...