Component Replacement
You can replace some ABP components with your custom components.
The reason that you can replace but cannot customize default ABP components is disabling or changing a part of that component can cause problems. So we named those components as Replaceable Components.
How to Replace a Component
Create a new component that you want to use instead of an ABP component. Add that component to declarations
and entryComponents
in the AppModule
.
Then, open the app.component.ts
and execute the add
method of ReplaceableComponentsService
to replace your component with an ABP component as shown below:
How to Replace a Layout
Each ABP theme module has 3 layouts named ApplicationLayoutComponent
, AccountLayoutComponent
, EmptyLayoutComponent
. These layouts can be replaced the same way.
A layout component template should contain
<router-outlet></router-outlet>
element.
The example below describes how to replace the ApplicationLayoutComponent
:
Run the following command to generate a layout in angular
folder:
Add the following code in your layout template (my-application-layout.component.html
) where you want the page to be loaded.
Open app.component.ts
in src/app
folder and modify it as shown below:
If you like to replace a layout component at runtime (e.g: changing the layout by pressing a button), pass the second parameter of the
add
method ofReplaceableComponentsService
as true. DynamicLayoutComponent loads content using a router-outlet. When the second parameter of theadd
method is true, the route will be refreshed, so use it with caution. Your component state will be gone and any initiation logic (including HTTP requests) will be repeated.
Layout Components
How to Add a New Layout Component
To add a new layout component, you need to follow these steps:
Step 1: Create a New Angular Component
This component should have a 'router-outlet' for dynamic content loading. You can create a new component using the Angular CLI. Run the following command in your terminal:
This command will create a new component named new-layout
. Now, open the new-layout.component.html file and add a router-outlet
to it:
This 'router-outlet' will act as a placeholder that Angular dynamically fills based on the current router state.
note: (don't forget: you should add the app in the app.module.ts file)
Step 2: Define a Variable for the Layout Component
Although this step is optional, it can be useful if you're going to use the layout component's value multiple times. You can define a variable for the layout component like this:
In this variable, key
is a unique identifier for the layout component, and component
is the name of the layout component.
You can use this variable when you need to refer to the layout component.
Step 3: Add the Layout Component to the ABP Replaceable-System
Next, you need to add the new layout component to the ReplaceableComponentsService
. This service allows you to replace a component with another one dynamically.
You can do this by defining a provider for APP_INITIALIZER
that uses a factory function. In this function, you inject the ReplaceableComponentsService
and use its add
method to add the new layout component.
Here's how you can do it:
In this code, configureLayoutFn
is a factory function that adds the new layout component to the ReplaceableComponentsService
. The APP_INITIALIZER
provider runs this function when the application starts.
note: (don't forget: you should add the CUSTOM_LAYOUT_PROVIDERS in the app.module.ts file)
Step 4: Define the Application's Dynamic Layouts
Finally, you need to define the application's dynamic layouts. This is a map where the keys are the layout keys and the values are the layout components.
You can add the new layout to the existing layouts like this:
Step 5: Pass the Dynamic Layouts to the CoreModule
The final step is to pass the dynamic layouts to the provideAbpCore
using the withOptions
method. This method allows you to configure the module with a static method.
Here's how you can do it:
In this code, myDynamicLayouts
is the map of dynamic layouts you defined earlier. We pass this map to the provideAbpCore
using the withOptions
method.
Now that you have defined the new layout, you can use it in the router definition. You do this by adding a new route that uses the new layout.
Here's how you can do it:
How to Replace LogoComponent
Run the following command in angular
folder to create a new component called LogoComponent
.
Open the generated logo.component.ts
in src/app/logo
folder and replace its content with the following:
Open app.component.ts
in src/app
folder and modify it as shown below:
The final UI looks like below:
How to Replace RoutesComponent
Run the following command in angular
folder to create a new component called RoutesComponent
.
Open the generated routes.component.ts
in src/app/routes
folder and replace its content with the following:
Import the SharedModule
to the imports
array of AppModule
:
Open the generated routes.component.html
in src/app/routes
folder and replace its content with the following:
Open app.component.ts
in src/app
folder and modify it as shown below:
The final UI looks like below:
How to Replace NavItemsComponent
Run the following command in angular
folder to create a new component called NavItemsComponent
.
Open the generated nav-items.component.ts
in src/app/nav-items
folder and replace the content with the following:
Import the SharedModule
to the imports
array of AppModule
:
Open the generated nav-items.component.html
in src/app/nav-items
folder and replace the content with the following:
Open app.component.ts
in src/app
folder and modify it as shown below:
The final UI looks like below: