#13: π§ Refactor App Component
We're going to perform a small refactoring. The app-root
shouldn't have such a large template and all this logic. It should just call another component that will deal with that.
Create a new component called
list-manager
:
ng g c list-manager
Move all the code from
app-root
tolist-manager
.You can keep the title in app-root, and give it a nice value.
Be careful not to change the list manager component's class name!
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule],
template: `
<h1>
Welcome to {{ title }}!
</h1>
`,
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'My To-Do List App';
}
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TodoItem } from '../interfaces/todo-item';
@Component({
selector: 'app-list-manager',
standalone: true,
imports: [CommonModule],
template: `
<app-input-button-unit (submit)="addItem($event)"></app-input-button-unit>
<ul>
@for(let todoItem of todoList; track todoItem.title) {
<li>
<app-todo-item [item]="todoItem"></app-todo-item>
</li>
}
</ul>
`,
styleUrl: './list-manager.component.scss'
})
export class ListManagerComponent {
todoList: TodoItem[] = [
{title: 'install NodeJS'},
{title: 'install Angular CLI'},
{title: 'create new app'},
{title: 'serve app'},
{title: 'develop app'},
{title: 'deploy app'},
];
addItem(title: string) {
this.todoList.push({ title });
}
}
Call the new component from the
app-root
template:
template: `
<h1>
Welcome to {{ title }}!
</h1>
<app-list-manager></app-list-manager>
`,
That's it! Now we can go on.
Last updated