我正在尝试从Observable数组中删除元素,并利用退出动画。输入动画效果完美,但是退出动画不会触发,并且元素会突然删除。我在下面包含了我的代码。感谢您的帮助或建议!
ao-notifications.service.ts
import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject, timer } from 'rxjs';
import * as _ from 'lodash';
import Message from '../dependencies/Message';
import MessageOptions from '../dependencies/MessageOptions';
@Injectable({
providedIn: 'root'
})
export class AoNotificationsService {
id: BehaviorSubject<number> = new BehaviorSubject<number>(0);
messages: BehaviorSubject<Message[]> = new BehaviorSubject<Message[]>([]);
list(): Observable<Message[]> {
return this.messages;
}
emit({ content, style, delay }: MessageOptions) {
const id = this.id.getValue();
const message = new Message(id, content, style);
this.messages.next(_.sortBy(_.concat(this.messages.getValue(), message), (o: Message) => o.id).reverse());
if (delay) {
timer(delay).subscribe(() => this.purge(id));
}
this.id.next(id + 1);
}
purge(id: number) {
this.messages.next(_.filter(this.messages.getValue(), o => o.id !== id));
}
}
ao-notifications.component.html
<div class="wrapper">
<aside *ngFor="let message of (messages | async)">
<div
@messageState
class="notification"
[ngClass]="{
primary: message.style == 'primary',
secondary: message.style == 'secondary',
success: message.style == 'success',
danger: message.style == 'danger',
warning: message.style == 'warning',
info: message.style == 'info'
}"
>
<button class="dismiss" (click)="dismiss(message.id)">X</button> {{ message.content }}
</div>
</aside>
</div>
ao-notifications.component.ts
import { Component, OnInit } from '@angular/core';
import { trigger, style, animate, transition } from '@angular/animations';
import { Observable } from 'rxjs';
import Message from './dependencies/Message';
import { AoNotificationsService } from './services/ao-notifications.service';
@Component({
selector: 'ao-notifications',
templateUrl: './ao-notifications.component.html',
styleUrls: ['./ao-notifications.component.scss'],
animations: [
trigger('messageState', [
transition('void => *', [
style({
opacity: 0
}),
animate(
'1s ease-in-out',
style({
opacity: 1
})
)
]),
transition('* => void', [
animate(
'1s ease-in-out',
style({
opacity: 0
})
)
])
])
]
})
export class AoNotificationsComponent implements OnInit {
messages: Observable<Message[]>;
constructor(private notifications: AoNotificationsService) {}
ngOnInit() {
this.messages = this.notifications.list();
}
dismiss(id: number) {
this.notifications.purge(id);
}
}
答案 0 :(得分:0)
该问题是由于* ngFor在一边,而不是在调用@messageState的div上的事实。如果对其进行如下修改,则动画可以正常工作。
index.php