typescript 切换实现的条件导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35919878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:20:36  来源:igfitidea点击:

Conditional import to switch implementations

node.jstypescript

提问by Lukas Pirkl

I have node.js application written in TypeScript and I need to switch between two interface implementations based on the config file. Currently I have this code which seems to be working.

我有用 TypeScript 编写的 node.js 应用程序,我需要根据配置文件在两个接口实现之间切换。目前我有这个似乎正在工作的代码。

"use strict";

import { config } from "../config";

let Something;
if (config.useFakeSomething) {
    Something = require("./FakeSomething").FakeSomething;
} else {
    Something = require("./RealSomething").RealSomething;
}
...
let s = new Something();
s.DoStuff();
...

But I have bad gut feeling about that (mainly because of the mixing requireand importfor loading modules). Is there some other way how to achieve implementation switching based on config file without importing both modules?

但是我对此有不好的直觉(主要是因为加载模块的混合要求导入)。有没有其他方法可以在不导入两个模块的情况下实现基于配置文件的实现切换?

采纳答案by Calvin Belden

If you want to keep the client-code for your Somethingclass clean, you can move the conditional importing to a single file. You can have the following directory structure for your Something module:

如果您想保持类的客户端代码Something干净,您可以将条件导入移动到单个文件中。您的Something模块可以具有以下目录结构:

/Something
    RealSomething.ts
    FakeSomething.ts
    index.ts

And in your index.ts, you can have the following:

在你的 index.ts 中,你可以有以下内容:

import { config } from '../config';

const Something = config.useFakeSomething ?
    require('./FakeSomething').FakeSomething :
    require('./RealSomething').RealSomething;

export default Something;

And in your client code, you can just import Something:

在您的客户端代码中,您只需导入Something

import Something from './Something/index';

回答by Amid

I can see nothing wrong with your approach. In fact lines like

我看不出你的做法有什么问题。事实上,像

import { config } from "../config";

When targeting commonjs will be compiled to the following javascript (ES6):

当以 commonjs 为目标时,将被编译为以下 javascript (ES6):

const config = require('../config');

So they are effectively identical and you are not mixing different module loading techniques.

所以它们实际上是相同的,并且您不会混合不同的模块加载技术。

回答by Alexander Gharibashvili

You can do it like that:

你可以这样做:

let moduleLoader:any;

if( pladform == 1 ) {
    moduleLoader = require('./module1');
} else {
    moduleLoader = require('./module2');
}

and then

接着

if( pladform === 1 ) {
    platformBrowserDynamic().bootstrapModule(moduleLoader.module1, [ languageService ]);
}
else if ( pladform === 2 ) {
    platformBrowserDynamic().bootstrapModule(moduleLoader.module2, [ languageService ]);
}

回答by o.z

In addition to the correct answers above, in case you need this switching for many files within a single folder, you can use a symbolic-link (not available on Windows) that referenced to the right folder, so your code remains clean.

除了上面的正确答案之外,如果您需要对单个文件夹中的多个文件进行这种切换,您可以使用引用正确文件夹的符号链接(在 Windows 上不可用),因此您的代码保持干净。

This approach is good for switching between real code and stubs, for instance

例如,这种方法适用于在真实代码和存根之间切换