, , . RouterLink, , HomeComponent , .
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'route/:id',
component: HomeComponent
}
];
, DOM, HomeComponent , , , <div> (). , HomeComponent JavaScript.
@Component({
selector: 'home-component',
template: `
<a routerLink="/route/1">Link</a>
<div myDirective *ngIf="visible" @transition>Test</div>
<button (click)="visible = !visible">Toggle</button>
`,
animations: [
trigger('transition', [
transition(':enter', [
style({ opacity: 0 }),
animate('.35s ease', style({ opacity: '*' }))
]),
transition(':leave', [
style({ opacity: '*' }),
animate('.35s ease', style({ opacity: 0 }))
])
])
]
})
export class HomeComponent {
visible = false;
constructor() {
console.log('New instance')
}
}
, , , Toggle - .
@Component({
selector: 'app-toggle-button',
template: `
<div myDirective *ngIf="visible" @transition>Test</div>
<button (click)="visible = !visible">Toggle</button>
`,
animations: [
trigger('transition', [
transition(':enter', [
style({ opacity: 0 }),
animate('.35s ease', style({ opacity: '*' }))
]),
transition(':leave', [
style({ opacity: '*' }),
animate('.35s ease', style({ opacity: 0 }))
])
])
]
})
export class ToggleComponent {
visible = false;
}
app.component.html:
<app-toggle-button></app-toggle-button>
<router-outlet></router-outlet>
A SINGLE ToggleComponent, .
, , .