模块引用中的Typescript根符号

时间:2017-04-10 08:51:24

标签: typescript

在Typescript中是否有斜杠表示法来指示应用程序的根目录。例如。如果我有这样的子模块

root/common/mychildModule/myChildModule.ts

是否有引用根的根,例如

import { myRootModule} from 'rootnotation/my-root-module';

或者是使用../../../my-root-module执行此操作的唯一方法吗?

这是我的tsconfig.js

{
     "compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"noEmit": true,
"noEmitHelpers": true,
"importHelpers": true,
"strictNullChecks": false,
"baseUrl": "./src/app",
"lib": [
"dom",
"es6"
],
"typeRoots": [
  "node_modules/@types"
],
"types": [
  "hammerjs",
  "jasmine",
  "node",
  "source-map",
  "uglify-js",
  "webpack"
]
 },
   "exclude": [
"node_modules",
"dist"
],
"awesomeTypescriptLoaderOptions": {
"forkChecker": true,
"useWebpackText": true
},
 "compileOnSave": false,
  "buildOnSave": false,
 "atom": {
"rewriteTsconfig": false
 }
 }

我还有一个用于webpack的tsconfig

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "noEmit": true,
    "noEmitHelpers": true,
    "importHelpers": true,
    "strictNullChecks": false,
    "baseUrl": "./src/app/",
    "lib": [
      "es2015",
      "dom"
    ],
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "hammerjs",
      "node"
    ]
  },
  "exclude": [
    "node_modules",
    "dist",
    "src/**/*.spec.ts",
    "src/**/*.e2e.ts"
  ],
  "awesomeTypescriptLoaderOptions": {
    "forkChecker": true,
    "useWebpackText": true
  },
  "angularCompilerOptions": {
    "genDir": "./compiled",
    "skipMetadataEmit": true
  },
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

1 个答案:

答案 0 :(得分:1)

您可以在tsconfig.json

中定义{ "compilerOptions": { "baseUrl": "." } }
baseUrl

您可以在任何地方导入模块,就像您在import { myRootModule } from 'my-root-module'; 中一样。在你的情况下,你可以这样做:

npm install jquery --save

文档:https://www.typescriptlang.org/docs/handbook/module-resolution.html