Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
Passing void events from parent component to child component

Quick overview

I have a "Terminal" Component which should be able to be used multiple times all over my application. This component should also be able to put into a "read only" mode where you pass a single command you'd like the terminal to fire and it will display the output. I am trying to have this component be able to be refreshed by many things; data updating else where, user events, etc. To achieve this, currently, I am using RxJS subjects as an @Input into the terminal component which when updated fires the subscribed functions. This works for the first user click (see bellow) but after that the subject doesn't update again. I suspect this is due to the "object" not updating, there for angular doesn't register the change and my whole idea falls apart.

Can I fix this? or do I need to redesign this "Terminal" component?

Code

terminal.component.ts

export class TerminalComponent implements OnInit, OnDestroy {
    constructor() {}

    $destroy = new Subject();
    terminalOutput = '';

    // Command input (if you want the terminal to only fire one command)
    @Input() command = '';
    $command: BehaviorSubject<string> = new BehaviorSubject('');

    // Refresh terminal input
    $refresh: Subject<void> = new Subject();
    @Input() set refresh(value: Subject<void>) {
        this.$refresh = value;
    }

    // ReadOnly Input
    @Input() readOnly = false;

    ngOnInit(): void {
        this.$refresh
            .pipe(
                takeUntil(this.$destroy),
                tap(() => {
                    const lastCommand = this.$command.getValue();
                    if (lastCommand) {
                        console.log('Refreshing, last command is:', lastCommand);                       
                    }
                })
            )
            .subscribe();

        //...
    }

    //...
}

parent.component.html

<h1>Home</h1>

<app-terminal command="ls" [refresh]="$refreshSubject"></app-terminal>

<button (click)="refreshTest()">Refresh</button>

parent.component.ts

export class ParentComponent implements OnInit {
    $refreshSubject: Subject<void> = new Subject();

    constructor() {}

    ngOnInit(): void {}

    refreshTest(): void {
        console.log('Refreshing');
        this.$refreshSubject.next();
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So I found the problem, it was another bug in my code that was causing the RxJS tap to not fire after the first time.

 ngOnInit(): void {
        this.$refresh
            .pipe(
                takeUntil(this.$destroy),
                tap(() => {
                    const lastCommand = this.$command.getValue();
                    if (lastCommand) {
                        console.log('Refreshing, last command is:', lastCommand);                       
                        throw new Error("Example Error"); 
                        // ^^^^ This will cause future Subject emits to not fire as this function has failed.
                    }
                })
            )
            .subscribe();

        //...
    }

Side note: I think the question title should be changed to better suit the real problem with my code, and there for have better SEO. However if this is a completely rubbish question then I think it should be deleted.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda