Boa tarde,
estou tendo um problema quando ao tentar usar o toast do PrimeNG, quando não há redirecionamento, ele funciona normalmente, porém quando preciso redirecionar para outra página, o mesmo já não aparece.
base-resource.form.component.ts
export abstract class BaseResourceFormComponent implements OnInit, AfterContentChecked {
currentAction: string; messageSuccess: ToastMessage = { severity: ‘success’, summary: ‘Success’, detail: ‘Message Content’ } as ToastMessage; messageWarn: ToastMessage = { severity: ‘warn’, summary: ‘Success’, detail: ‘Message Content’ } as ToastMessage; messageinfo: ToastMessage = { severity: ‘info’, summary: ‘Success’, detail: ‘Message Content’ } as ToastMessage; messageError: ToastMessage = { severity: ‘error’, summary: ‘Success’, detail: ‘Message Content’ } as ToastMessage; messageService: MessageService;constructor( protected injector: Injector, ) { this.messageService = this.injector.get(MessageService); }protected setCurrentAction() { if (this.route.snapshot.url[0].path === ‘new’) { this.currentAction = ‘new’; } else { this.currentAction = ‘edit’; } }protected salvarResource() { this.messageSuccess = { severity: ‘success’, summary: ‘Salvo Com Sucesso’, detail: ‘Salvo Com Sucesso’ } this.resourceService.salvar(this.resource).subscribe( resource => this.actionsForSucess(resource), error => this.actionForError(error), ); }protected actionsForSucess(resource: T): void { let baseComponentPath = ‘’; if (this.currentAction === ‘new’) { baseComponentPath = this.router.url; } else { const posicoes: string[] = this.router.url.split(’/’); for (let i = 1; i < posicoes.length - 2; i++) { baseComponentPath = baseComponentPath + ‘/’ + posicoes[i]; } baseComponentPath = baseComponentPath + ‘/new’; } this.router.navigateByUrl(’/’, { skipLocationChange: true }).then( () => this.router.navigateByUrl(baseComponentPath)); this.messageService.add(this.messageSuccess); }
*Deixei só a parte do código que usa o MessageService, se não ia ficar muito grande.
*Esses if’s no actionForSucess são para manipular a URL e redirecionar o usuário para um novo cadastro.
*Nessa parte, caso o usuário esteja na página de novo cadastro, o toast funciona (porque redireciona para a mesma URL), agora se ele estiver na de edição, o toast não aparece.