2016-09-17 40 views
1

目前我有使用相對路徑我的模塊導入:如何配置Typescript以便「知道」我的SystemJS路徑?

import {UserService} from "../../../services/user-service"; 

不過,我寧願配置SystemJS以致於分辨率開始在應用程序的根目錄,除非模塊路徑與./開始,然後我可以做這(services位於根):

import {UserService} from "services/user-service"; 

如果我想從當前位置導入文件:

import {Something} from "./something"; 

我可以配置SystemJS第一部分:

System.config({ 
    defaultJSExtensions: true, 
    transpiler: false, 
    paths: { 
     "*": "dist/*", 
     "services": "dist/services/*", 
     "models": "dist/models/*", 
     "github:*": "jspm_packages/github/*", 
     "npm:*": "jspm_packages/npm/*" 
    }, 
    // ... 
} 

但是......

  1. 我不知道如何使打字稿知道我SystemJS.paths配置,並
  2. 我不不知道如何配置SystemJS以「默認從根加載東西」。

回答

0

一個解決方案是在SystemJs中配置一個包併爲typescript添加路徑映射。

您可以像服務目錄下添加index.ts文件做到這一點:

export { UserService } from './user-service'; 
export { OrderService } from './order-service'; 
... 

以下內容添加到您的SystemJs配置(參見:SytemJS config-api):

SystemJS.config({ 
    ... 
    packages: { 
    "services": { 
     "main": "index.ts" 
    } 
    } 
}); 

添加此到tsconfig.json(參見:Typescript module resolution):

{ 
    "compilerOptions": { 
    { 
    ... 
    "moduleResolution": "Node", 
    "baseUrl": ".", 
    "paths": { 
     "services": [ "services" ] 
    } 
    } 
} 

現在你可以現在導入一個文件,如:

import { UserService } from 'services'; 

這將在Visual Studio中提供intellisense並允許在瀏覽器中加載模塊。

相關問題