无法通过构造函数将组件注入组件?

时间:2018-03-03 18:51:21

标签: angular typescript constructor

我正在尝试呼叫私人登录:LoginComponent,但是,当我添加私人登录时,我的网站不会加载:LoginComponent。在我将Login添加到构造函数之前,我的网站加载并运行正常。我究竟做错了什么?我可以不拨打私人登录:LoginComponent吗?我已在下面附上课程LoginComponent

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
    ) { }
}

我试着打电话给这个班:

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
    ) { }
}

2 个答案:

答案 0 :(得分:2)

组件之间传递的任何数据都可以通过两种方法进行:

  1. 创建服务并在两个组件中注入该服务。组件之间共享的数据是通过服务完成的。
  2. 在一个组件中创建Subject,在另一个组件中创建subscribe
  3. 无法通过构造函数将一个组件注入另一个组件。

答案 1 :(得分:0)

您可以使用依赖注入调用另一个组件的任何函数或变量。这是一个例子。

expr