typescript:从外部路径导入模块

时间:2017-07-20 11:06:58

标签: angular typescript

我有以下结构(编辑):

workspace
|- thegame
 |- node_modules
 | package.json
 | bs-config.json
 |- src
  | tsconfig.json
  |- app
   | game.model.ts (<--  here I would like to import game-engine)
|- game-engine
 |- dist (after the local build)
 | package.json
 | tsconfig.json
 |- lib
  | index.ts (the actual engine module)

我在&#34; thegame&#34;中运行应用程序(Angular2) &#34; npm start&#34;。

我应该在thegame / src / tsconfig.json中添加什么,以便我可以在game.model.ts中执行以下操作?

import { Engine } from 'game-engine';

我试图将游戏引擎符号链接到#34; to&#34; thegame / node_modules&#34;但当我用lightserve运行项目时,它会给出#34; 404 GET / game-engine&#34;

我想开发与Web应用程序分离的引擎。我也对任何其他提示如何实现这一点感兴趣。

该项目基于https://github.com/angular/quickstart

的Angular 2快速入门

1 个答案:

答案 0 :(得分:2)

由于您使用的是使用SystemJS和NPM作为其基础的Angular快速入门工具包,并且您已经了解了如何描述目录布局,因此您需要执行以下步骤。

workspace/thegame中打开一个终端并运行

npm install --save ../game-engine

这将告诉npm从磁盘上的该位置安装软件包,作为thegame应用程序的标准依赖项。

现在我们只需要告诉SystemJS新添加的依赖项所在的位置。 SystemJS使用显式配置(而不是递归目录遍历)来定位依赖项。这是的事情,但由于我们使用的是NPM而不是JSPM,我们需要手动设置它。

幸运的是,它相当于添加到systemjs.config.js文件中的一行,根据快速入门,该行应位于src目录的thegame子目录中。

将以下内容添加到配置对象的"map"属性中:

"game-engine": "npm:game-engine/dist/index.js"

最终文件看起来像

SystemJS.config({
  paths: {
    "npm:": "node_modules/"
  },
  map: {
    "game-engine": "npm:game-engine/dist/index.js",
    "@angular/core": "npm:@angular/core/bundles/core.umd.js",
    // etc.
  }
  // etc.
});
相关问题