#Parent component not providing facade to child

18 messages · Page 1 of 1 (latest)

cyan pulsar
#

I am refactoring a functionality and created a facade that will orchestrate the behavior of the child and parent components.
Since I don't want the facade to be global I didn't add @Injectable({ providedIn: 'root' }) and instead used @Injectable().
Now I am providing it in the parent component in its providers array and injecting in the constructor in both the parent and the child, however I still get an error about no provider being found.
How can this be resolved?

signal beacon
#

You'll have to show some code. this should work

cyan pulsar
#

@signal beacon This is the code, I redacted some stuff that shouldn't be needed (I think) to analyze what is happening.
If more information is needed please tell.

This is the facade

@Injectable()
export class InvolvedPartyFacade {
  constructor(...) { ... }
}

This is the manage component (child) ts code.

@Component({
  selector: 'nlf-cla-involved-party-manage',
  templateUrl: './nlf-cla-involved-party-manage.component.html',
  imports: [
    ReactiveFormsModule,
    FormsModule,
  ]
})
export class NlfClaInvolvedPartyManageComponent implements OnChanges {
  constructor(
    private fb: FormBuilder,
    @Host() @SkipSelf() private facade: InvolvedPartyFacade
  ) {
    super('NlfClaInvolvedPartyManageComponent');
  }
}

This is the creation component (parent) template and ts code.

@if (hasManagerRole()) {
  @if (configErrorMessages.length === 0 && isClaimAllocated) {
    <nlf-cla-involved-party-manage
      #involvedPartyManage
    ></nlf-cla-involved-party-manage>
  } @else {
    ...
  }
}

@Component({
  selector: 'nlf-cla-subclaim-involved-party-create',
  templateUrl: './nlf-cla-subclaim-involved-party-create.component.html',
  imports: [NlfClaInvolvedPartyManageComponent],
  providers: [InvolvedPartyFacade],
})
export class NlfClaSubclaimInvolvedPartyCreateComponent
  implements OnChanges {

  constructor(private redirectUtils: NLFClaRedirectUtils, private facade: InvolvedPartyFacade) {
    super('NlfClaSubclaimInvolvedPartyCreateComponent');
  }
}
signal beacon
#

I am not sure exactly what @Host plus @SkipSelf achieves. Does it work without them?

upper jay
#

Those should be needed for a situation where you're constructing a nested structure. So ComponentA projected in another instance of ComponentA (a tree basically)
Shown code looks not the case, considering that parent and child are different classes.
I'm not too sure about that super(...) call either...

cyan pulsar
cyan pulsar
signal beacon
#

It's weird that the injection in NlfClaSubclaimInvolvedPartyCreateComponent works, but supposedly not in its children. That goes contrary to how DI works with components.

cyan pulsar
#

Yeah, I'm finding it weird because according to the docs this should be working....

signal beacon
#

Try creating a minimal sharable reproduction (as outlined in #how-to-get-help). This may already lead you to the problem in your code.

cyan pulsar
#

So I got it working after some more trial and error and ending up removing @host and @skipself.
It appears I imagined it didn't work when I didn't have those annotations or something because now it works and I can't replicate the error.
Thank you @signal beacon and @upper jay for the help.

dusty pewter
signal beacon
dusty pewter
#

Cool, I didn't use standalone components yet so I thought that we need to add service to all 3 components providers, but with that way each component would create new instance of service and not share state between right?

signal beacon
dusty pewter
#

In what case we should use skipSelf? It's used when we want Angular to search for providers in parent upper components right? But what would be use case?

signal beacon
#

You can use it for example to check if a component is nested within itself.
For example, Angular CDK uses it to detect when you nest dialogs.

#

Because if you just did inject(Dialog) within Dialog, you'd just get this