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

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

问题描述

这似乎是不允许的.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天全站免登陆