-
Notifications
You must be signed in to change notification settings - Fork 0
1. Standalone Components
Andrei Antal edited this page Oct 24, 2024
·
2 revisions
Currently our application is build using modules. Four our first challenge we will update the architecture of our app and use standalone components. A few steps are needed to achieve this:
- make components standalone
- remove modules
- update bootstrapping and other settings (like routing)
-
Currently the main configurations of the app (base routing, including the
HttpClientModule) are defined in theapp.module.tsfile. We will move this configuration to a new file and remove theAppModulecompletely since it will not be needed anymore, in the context of standalone components. -
Create a new file
app.config.tsin thesrcfolder. Add a configuration object with the following properties:
const routes: Route[] = []
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(), // <-- new http configuration
provideRouter(routes) // <-- new router configuration - keep the routes empty for now
]
}- The
AppComponentis the root component of our application. We need to convert it to a standalone component and update the imports accordingly.
@Component({
selector: 'ngm-root',
standalone: true, // <-- new property, make component standalone
imports: [MovieListComponent, RouterOutlet, RouterLink],, // <-- new imports
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})- We will bootstrap the application using the
bootstrapApplicationmethod from@angular/platform-browserand theappConfigobject we created in the previous step.
bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err)
);- Router configurations for the movie feature are listed in the
movies.module.tsfile. We will move them a separate file and use them in theapp.config.tsfile. - Create a new file
movies.routes.tsin themoviesfolder and update the routes configuration usingloadComponentfor each route:
export const movieRoutes: Route[] = [
{
path: '',
loadComponent: () =>
import('./components/movie-list/movie-list.component').then(
(c) => c.MovieListComponent
),
},
{
path: 'new',
loadComponent: () =>
import('./components/movie-detail/movie-detail.component').then(
(c) => c.MovieDetailComponent
),
},
{
path: ':id',
loadComponent: () =>
import('./components/movie-detail/movie-detail.component').then(
(c) => c.MovieDetailComponent
),
},
];- Update the
app.config.tsfile to use the new routes configuration:
const routes: Route[] = [
{ path: '', component: HomeComponent },
{
path: 'movies',
loadChildren: () =>
import('./movies/movies.routes').then((m) => m.movieRoutes),
},
];- Update the
HomeComponent,MovieListComponent,MovieItemandMovieDetailComponentto use the new standalone component configuration, similar to theAppComponent:- add
standalone: trueto the@Componentdecorator of each component - update the
importsproperty to include the necessary modules and components, based on what's being used in the template
- add
- You can now safely remove the
AppModuleandMoviesModulemodules.
Rerun the app and make sure everything works as before.