0

I am trying to call private login: LoginComponent, however, my website wont load when I add private login: LoginComponent. My website load and runs fine before I add Login to the Constructor. What am I doing wrong? Can I not call private login: LoginComponent? I have attached class LoginComponent below.

import { Component, OnInit, ViewChild } from '@angular/core'
import { Router } from '@angular/router'
import * as Rx from "rxjs"
import { environment } from "../../../environments/environment"
import {LoginComponent} from "app/components/login/login.component"
import { AuthService } from "../../services/auth/auth.service"
import { LoginService } from "../../services/login/login.service"
import { SageApiService } from "../../services/sage-api/sage-api.service"
import { DataModel } from "../../model/data-model"
// wvj contract

@Component({
    selector: 'app-contract-form',
    templateUrl: './contract-form.component.html',
    styleUrls: ['./contract-form.component.css']
})
export class ContractFormComponent implements OnInit {

    @ViewChild('staticModal') staticModal;

    dataModel: DataModel = new DataModel()

    constructor(
        private router: Router,
        private authService: AuthService,
        private loginService: LoginService,
        private sageApi: SageApiService,
        private login: LoginComponent
    ) { }
}

I am trying to call this class:

import { Component, OnInit } from '@angular/core';
import {Router} from "@angular/router";
import { LoginService } from "../../services/login/login.service";
import { AuthService } from "../../services/auth/auth.service"

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

    error: string = "";
    username: string = "";
    password: string = "";
    isLoading: boolean = false;
    constructor(
        private loginService: LoginService,
        private authService: AuthService,
        private router: Router
    ) { }
}
9
  • 1
    Define "blow up". Precisely. Post the code that "blows up". A class can't be called. Only functions/methods/constructors can be. Commented Mar 3, 2018 at 18:52
  • My website won't load. It loads perfectly fine before I add LoginComponent. I am trying to return a value on LoginComponent to ContractFormComponent Commented Mar 3, 2018 at 18:55
  • 3
    Why are you trying to inject a component via the constructor? Commented Mar 3, 2018 at 19:02
  • 1
    Stop describing your code in a vague way. Post it instead. Commented Mar 3, 2018 at 19:04
  • 2
    @CC41325 In that case, either do so via service, or do so using an EventEmitter Commented Mar 3, 2018 at 19:09

2 Answers 2

5

Any data passing between components can take place by 2 methods:

  1. Create a service and inject that service in both components. The data to be shared between the components is done via the service.
  2. Create a Subject in one component and subscribe to it in the other component.

One component cannot be injected via constructor into another component.

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

1 Comment

You can actually inject components which are in the same view hierarchy: angular.io/guide/dependency-injection-navtree
1

You can call any function or variable of another component using dependency injection. Here is an example.

import { LoginComponent } from "app/components/login/login.component"

export class ContractFormComponent implements OnInit {

  loginComponent: LoginComponent

  constructor( private _login: LoginComponent ) {
    this.loginComponent = _login;
  }

  callAnythingFromLoginComponent() {
    this.loginComponent.anyFunctionFromLoginComponent();
  }
}

Comments

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.