在参数中,透传功能不起作用

huangapple go评论71阅读模式
英文:

Passthrough function in parameters not working

问题

当我单击 snackbar 中的按钮时,我看到 callButtonAction() 中的 console.log(1),但 this.close() 没有被触发。我在这里漏掉了什么?

英文:

I have an array of announcements, each announcement shows up in a snackbar and has some text and a button:

// announcements.services.ts
notificationConfig: {
	buttons: [
		{
			text: this.translateService.instant(_('Go to Insights dashboard')),
			action: () => {
				this.router.navigate(['/dm/statistics/insights']);
				this.dismissAnnouncement(AnnouncementName.INSIGHTS_DASHBOARD);
			},
		},
	],
},
// toast-notification.html
<div *ngFor="let button of buttons" class="buttonWrapper">
	<app-button (click)="callButtonAction(button.action)">{{ button.text | translate }}</app-button>
</div>
// toast-notification.component.ts 
close() {
	console.log(2);
	this.onClose.emit();
}

callButtonAction(action: (closeHandler: CloseHandler) => void) {
	console.log(1);
	action(() => this.close());
}

When I click the button in the snackbar I do the see the console.log(1) from the callButtonAction() but the this.close() is not being fired.

What I am missing here?

答案1

得分: 2

看起来你没有使用action中提供的处理程序,这是你的代码示例:

notificationConfig: {
    buttons: [
        {
            text: this.translateService.instant('转到洞察仪表板'),
            action: (callback) => {
                this.router.navigate(['/dm/statistics/insights']);
                this.dismissAnnouncement(AnnouncementName.INSIGHTS_DASHBOARD);
                callback()
            },
        },
    ],
},
英文:

Looks like you are not using the handler given to action in action(() => this.close()).

Try this:

notificationConfig: {
    buttons: [
        {
            text: this.translateService.instant(_('Go to Insights dashboard')),
            action: (callback) => {
                this.router.navigate(['/dm/statistics/insights']);
                this.dismissAnnouncement(AnnouncementName.INSIGHTS_DASHBOARD);
                callback()
            },
        },
    ],
},

huangapple
  • 本文由 发表于 2023年2月6日 17:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定