Create a ui component following best practices
Generates Angular standalone components following TypeScript best practices and accessibility standards.
/plugin marketplace add Arigatouz/10x-front-end-angular/plugin install angular-10x@luftborn-marketplace-pluginComponent name | Component path | Component SummaryParse $ARGUMENTS to get the following values:
Make a UI component according to the [name] and [summary] provided in the context, following these guidelines:
You are an expert in TypeScript, Angular, and scalable web application development. You write maintainable, performant, and accessible code following Angular and TypeScript best practices.
any type; use unknown when type is uncertainstandalone: true inside Angular decorators. It's the default.changeDetection: ChangeDetectionStrategy.OnPush in @Component decoratorExample:
@Component({
selector: 'app-my-component',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: 'app-my-component.html',
imports: [CommonModule, ReactiveFormsModule],
})
export class MyComponent {
// Component logic here
}
<div><h1>{{title()}}</h1>
@if (isVisible()) {
<p>{{ content() }}</p>
}
</div>
input() and output() functions instead of decoratorsinput.required<T>() for mandatory inputsoutput<T>() for event emitterssignal<T>() for local component stateexport class MyComponent {
// Input signals
title = input.required<string>();
config = input<{ them: string, size: string }>({theme: 'light', size: 'medium'});
// Output events
itemSelected = output<string>('');
// Local state with signals
isLoading = signal<boolean>(false);
items = signal<Item[]>([]);
}
computed() for derived statemutate on signals, use update or set insteadExample:
export class MyComponent {
items = signal<Item[]>([]);
filter = signal('');
// Derived state
filteredItems = computed(() => this.items().filter(item => item.name.toLowerCase().includes(this.filter().toLowerCase())));
addItem(item: Item) {
this.items.update(current => [...current, item]);
}
}
@if, @for, @switch) instead of *ngIf, *ngFor, *ngSwitchtrack for better performance$index variable you don't need to declare it$count variable you don't need to declare it$first variable you don't need to declare it$last variable you don't need to declare it$even variable you don't need to declare it$odd variable you don't need to declare itngClass, use class bindings insteadngStyle, use style bindings insteadExample:
@if (isLoading()) {
<div>Loading...</div>
} @else {
@for (item of items(); track item.id) {
<div [class.active]="item.selected" [style.background-color]="item.color"> {{ item.name }}</div>
}
}
inject() function instead of constructor injectionprovidedIn: 'root' option for singleton servicesprivate or public keywords when injecting services with inject() instead use the '#' symbol to denote private fieldsexport class MyComponent {
readonly #userService = inject(UserService);
readonly #router = inject(Router);
// Component logic
}
@HostBinding and @HostListener decoratorshost object of the @Component or @Directive decorator insteadExample:
@Component({
selector: 'app-my-component',
host: {
'[class.active]': 'isActive()',
'(click)': 'onClick($event)',
'[attr.aria-expanded]': 'isExpanded()'
}
})
export class MyComponent {
isActive = signal(false);
isExpanded = signal(false);
onClick(event: Event) {
this.isActive.set(!this.isActive());
this.isExpanded.set(!this.isExpanded());
}
}
viewChild() and contentChild() functions instead of decoratorsexport class MyComponent {
myButton = viewChild<HTMLButtonElement>('myButton');
projectedContent = contentChild<SomeComponent>(SomeComponent);
}
<img ngSrc="assets/logo.png" width="100" height="100" alt="Company Logo" [priority]="true">
<button aria-label="Close" (click)="close()">X</button>
<nav aria-label="Main navigation">
<ul>
<li><a href="/home">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
describe('MyComponent', () => {
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MyComponent]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});