6

I am facing one issue while importing standalone component within each other.

Component A.

@Component({
  standalone: true,
  selector: 'app-a',
  templateUrl: './a.component.html',
  styleUrls: ['./a.scss'],
  imports: [
    BComponent
  ]
})
export class AComponent implements OnInit {

}

Component B

@Component({
  standalone: true,
  selector: 'app-b',
  templateUrl: './b.component.html',
  styleUrls: ['./b.scss'],
  imports: [
    AComponent
  ]
})
export class BComponent implements OnInit {

}

Earlier this solution was working fine for me as accessing these components with using templates. but now getting this Error.

Ref Error: Cannot access 'AComponent' before initialization.

Any help in above context is highly appreciated.

1 Answer 1

13

Pick a component and use forwardRef

@Component({
  standalone: true,
  selector: 'app-b',
  templateUrl: './b.component.html',
  styleUrls: ['./b.scss'],
  imports: [
    forwardRef(() => AComponent))
  ]
})
export class BComponent implements OnInit {

}

Stackblitz

Credit to El Greco / Tim Deschryver, twitter thread

Sign up to request clarification or add additional context in comments.

3 Comments

what if AComponent need to import in mutiple standalone component? should we need to use forwardRef in all other standalone component?
Saved me! Ive spent weeks trying to figure this out. I owe you 1 bitcoin :>
How to use forwardRef() if I try to use the standalone component in other component which is not standalone?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.