我写这个答案是因为我在语法上有点挣扎,而且我喜欢假人的例子,但 Günter 的答案是正确的。
我必须做的:
@Component({
selector: 'app-sidenav',
templateUrl: './sidenav.component.html',
styleUrls: ['./sidenav.component.scss'],
animations: [
trigger('toggleDrawer', [
state('closed', style({
transform: 'translateX(0)',
'box-shadow': '0px 3px 6px 1px rgba(0, 0, 0, 0.6)'
})),
state('opened', style({
transform: 'translateX(80vw)'
})),
transition('closed <=> opened', animate(300))
])
]
})
export class SidenavComponent implements OnInit {
private state: 'opened' | 'closed' = 'closed';
// binds the animation to the host component
@HostBinding('@toggleDrawer') get getToggleDrawer(): string {
return this.state === 'closed' ? 'opened' : 'closed';
}
constructor() { }
ngOnInit(): void {
}
// toggle drawer
toggle(): void {
this.state = this.state === 'closed' ? 'opened' : 'closed';
}
// opens drawer
open(): void {
this.state = 'opened';
}
// closes drawer
close(): void {
this.state = 'closed';
}
}