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

问题描述 投票:0回答:1

我试图打电话给私人登录: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
    ) { }
}
angular typescript constructor
1个回答
2
投票

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

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

一个组件不能通过构造函数注入另一个组件。

© www.soinside.com 2019 - 2024. All rights reserved.