打字稿外部模块可以具有循环依赖吗? [英] Can typescript external modules have circular dependencies?

查看:77
本文介绍了打字稿外部模块可以具有循环依赖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来这是不允许的. requireJS在以下内容上引发错误(这篇文章有所不同,因为它是通过内部模块解决的):

It looks like this is not allowed. requireJS is throwing an error on the following (this post is different as it was resolved with internal modules):

element.ts:

element.ts:

import runProperties = require('./run-properties');
export class Element {
   public static factory (element : IElement) : Element {

        switch (element.type) {
            case TYPE.RUN_PROPERTIES :
                return new runProperties.RunProperties().deserialize(<runProperties.IRunProperties>element);
        }
        return null;
    }
}

run-properties.ts:

run-properties.ts:

import element = require('./element');

export class RunProperties extends element.Element implements IRunProperties {
}

推荐答案

否,除非模块位于同一文件中,否则它们不能具有循环依赖项.每个文件都按顺序,同步进行处理,因此在转到第二个文件时,完整的文件定义(例如包括所有导出)尚未完成,第二个文件立即尝试要求/引用第一个文件,依此类推. .

No, modules can't have circular dependencies unless they are in the same file. Each file is being processed in sequence, synchronously, so the full file definition (including all of the exports for example) hasn't been completed when it goes to second file, which immediately tries to require/reference the first file, and so on.

通常,您可以通过将接口或基类引入公共定义文件(仅用于基本接口),并让其他文件将其用作公共接口",而不是直接引用这些类,从而打破循环依赖关系. .这是许多平台中的典型模式.

Normally, you can break a circular dependency by introducing an interface or base class into a common definition file(s) (basically interfaces only) and having the other files use that as a common "interface" rather than directly referencing the classes. This is a typical pattern in many platforms.

这篇关于打字稿外部模块可以具有循环依赖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆