Reputation: 53
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
) { }
}
Upvotes: 0
Views: 7737
Reputation: 41
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();
}
}
Upvotes: 1
Reputation: 740
Any data passing between components can take place by 2 methods:
Subject
in one component and subscribe
to it in the other component.One component cannot be injected via constructor into another component.
Upvotes: 5