向宿主元素添加 angular 动画
adding an angular animation to a host element
我通过
给宿主添加了一个动画
@Component({
....,
animations: [
trigger('slideIn', [
...
])
],
host: {
'[@animation]': 'condition'
}
}
效果很好,在编译时我被告知这已被弃用,我应该使用@HostBinding ...
@HostBinding('[@animation]') get slideIn() {
return condition;
}
这会给我一个错误
Can't bind to '[@animation' since it isn't a known property of 'my-component-selector'.
但是我无法将动画添加到我的模块中。我该怎么办?
@HostBinding()
不需要方括号
@HostBinding('@slideIn') get slideIn() {
有两个装饰器 @HostBinding()
和 @HostListener()
因此 ()
和 []
之间的区别是不必要的,而 host: [...]
是使用过。
我写这个答案是因为我在语法上遇到了一些困难,而且我喜欢傻瓜的例子,但 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';
}
}
我通过
给宿主添加了一个动画@Component({
....,
animations: [
trigger('slideIn', [
...
])
],
host: {
'[@animation]': 'condition'
}
}
效果很好,在编译时我被告知这已被弃用,我应该使用@HostBinding ...
@HostBinding('[@animation]') get slideIn() {
return condition;
}
这会给我一个错误
Can't bind to '[@animation' since it isn't a known property of 'my-component-selector'.
但是我无法将动画添加到我的模块中。我该怎么办?
@HostBinding()
@HostBinding('@slideIn') get slideIn() {
有两个装饰器 @HostBinding()
和 @HostListener()
因此 ()
和 []
之间的区别是不必要的,而 host: [...]
是使用过。
我写这个答案是因为我在语法上遇到了一些困难,而且我喜欢傻瓜的例子,但 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';
}
}