简体   繁体   English

角度/打字稿中的循环依赖

[英]Circular dependency in angular/typescript

I was facing circular dependency in angular project.我在 angular 项目中面临循环依赖。 I have come accross many solution including export all the dependent classes from "single file" as instructed here https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de It did't work.我遇到了许多解决方案,包括按照此处的说明从“单个文件”导出所有依赖类https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and- for-all-in-javascript-typescript-a04c987cf0de它没有用。 So I moved to different solution like using dependency injections as instructed in the following links:所以我转向不同的解决方案,比如按照以下链接中的说明使用依赖注入:

How to solve the circular dependency Services depending on each other 如何解决循环依赖服务相互依赖

But despite of using dependency injections, there are still exceptions.但是尽管使用了依赖注入,仍然有例外。 below is the code:下面是代码:

moduleA.ts模块A.ts

import { MODULE_B_NAME } from "./moduleB";
import { Injectable, Injector } from "@angular/core";


export const MODULE_A_NAME = 'Module A';
@Injectable({
  providedIn: 'root'
})
export class ModuleA {

  private tempService: any;
  constructor(private injector: Injector) {
    setTimeout(() => this.tempService = injector.get(MODULE_B_NAME));

  }


  public  getName(): string {

    this.tempService.getName();
    return "we are forked";
  }

}

moduleB.ts模块B.ts

import { MODULE_A_NAME } from "./moduleA";
import { Injectable, Injector } from "@angular/core";

export const MODULE_B_NAME = 'Module B';
@Injectable({
  providedIn: 'root'
})
export class ModuleB {

  private tempService: any;
  constructor(private injector: Injector) {

    setTimeout(() => this.tempService = injector.get(MODULE_A_NAME));


  }
  public getName(): string {

    //this.tempService = this.injector.get(MODULE_A_NAME);
    this.tempService.getName();
    return "we are forked";
  }

}

appComponent.ts应用组件.ts

import { Component } from '@angular/core';
import { ModuleA } from './moduleA';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'test01';


  getSomething() {

    return ModuleA.name;
  }



}

appModules.ts应用模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { ModuleA } from './moduleA';
import { ModuleB } from './moduleB';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [ModuleA, ModuleB],
  bootstrap: [AppComponent]
})
export class AppModule { }

Can someone pls look at the code and identity what is wrong with it thanks有人可以看看代码和身份有什么问题吗谢谢

在此处输入图像描述

The problem is that you have your module name exports in the same file as the module itself.问题是您的模块名称导出与模块本身在同一个文件中。 You should create a separate file named module-names.const.ts :您应该创建一个名为module-names.const.ts的单独文件:

export const MODULE_A_NAME = 'Module A';
export const MODULE_B_NAME = 'Module B';

You can then import this file in both your modules, without a circular dependency:然后,您可以在两个模块中导入此文件,而无需循环依赖:

import { MODULE_A_NAME } from "./module-names.const";

import { Injectable, Injector } from "@angular/core";

@Injectable({
  providedIn: 'root'
})
export class ModuleB {
  constructor(private injector: Injector) {
    setTimeout(() => this.tempService = injector.get(MODULE_A_NAME));
  }
}

However, what is it that you are trying to do?但是,您想要做什么? It feels like you are doing things which you definitely should not be doing in angular.感觉就像你在做你绝对不应该在 angular 做的事情。 (or in any other programming environment for that matter). (或在任何其他编程环境中)。 I'm pretty sure your module name will be different once the application has been compiled using the --prod flag, and what ever it is you try to do, won't work anymore.我敢肯定,一旦使用--prod标志编译了应用程序,您的模块名称就会有所不同,并且无论您尝试做什么,都将不再起作用。

In your case, you need a third service.在您的情况下,您需要第三项服务。 One that injects both service A and service B. And this service C should handle the things you want to do一个同时注入服务A和服务B的服务。这个服务C应该处理你想做的事情

It's not recommend to use same type circular dependencies.不建议使用相同类型的循环依赖。 Instead of that you should use some service/interface to collaborate components to each other.取而代之的是,您应该使用一些服务/接口来相互协作组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM