打字稿:无法找到我的自定义库的模块

时间:2016-09-27 00:55:59

标签: angular typescript ionic2

问题

当我从' ng2-orm&#39 ;;

导入Config时,为什么我的ng2-orm软件包不被导入(或被vscode识别)?
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }   from './app.component';
import { Config } from 'ng2-orm'; // ng2-orm/Config won't work either

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

详情

我正在努力建立我自己的TS库,用于angular2和ionic2。我已经研究和搜索,似乎无法想出为什么会失败,这可能只是由于我的某种不完整性。

我知道它只是一个开始,但我想可以从项目内部导出的任何东西都可以访问。我需要一个ngModule定义吗?

我提供了package.json,一些文件夹结构,gulp(所以你可以看到它),以及tsconfig.json

一切都很好,等等。

但是当我在角度快速启动时安装它时,它说我尝试导入时无法找到ng2-orm。我将所有内容都装入了index.js,并且该软件包位于node_modules文件夹中。

我错过了systemjs.config.js需要的东西吗?我不确定。我有一个typings.json,但它基本上是空的,也许我在那里错过了一些东西?

index.ts

export * from './Config/Config';

配置/ Config.ts

export class Config {
  public test(): boolean { return true; }
}

定义文件

index.d.ts

export * from './Config/Config';

Config.d.ts

export declare class Config {
    test(): boolean;
}

的package.json

{
  "name": "ng2-orm",
  "version": "0.0.1",
  "description": "An Angular2 ORM style data modeler to make your life easier for data management and versioning.",
  "main": "dist/js/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "typings": "./dist/definitions/**/*.d.ts",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ArchitectNate/ng2-orm.git"
  },
  "keywords": [
    "ng2",
    "angular2",
    "ionic2",
    "ORM",
    "SQLite",
    "WebSQL"
  ],
  "author": "Nathan Sepulveda <nathans@thephpeffect.com>",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/ArchitectNate/ng2-orm/issues"
  },
  "homepage": "https://github.com/ArchitectNate/ng2-orm#readme",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-typescript": "^3.0.1",
    "merge2": "^1.0.2",
    "tslint": "^3.15.1",
    "typescript": "^2.0.3",
    "typings": "^1.4.0"
  }
}

文件夹结构

enter image description here

gulpfile.js

我包括这个,所以你可以看到我的gulp将代码放入dist / js,dist / definitions和dist / maps

var gulp = require('gulp');
var ts = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var merge = require('merge2');

var tsProject = ts.createProject('tsconfig.json');

gulp.task('scripts', function() {
  var tsResult = tsProject.src()
    .pipe(sourcemaps.init())
    .pipe(tsProject());

  return merge([
    tsResult.dts.pipe(gulp.dest('dist/definitions')),
    tsResult.js
      .pipe(sourcemaps.write('../maps'))
      .pipe(gulp.dest('dist/js'))
  ]);
});

gulp.task('watch', ['scripts'], function() {
  gulp.watch('./src/**/*.ts', ['scripts']);
});

gulp.task('default', ['scripts', 'watch']);

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "outDir": "dist/js",
    "declaration": true
  },
  "filesGlob": [
    "src/**/*.ts",
    "!node_modules/**/*"
  ],
  "exclude": [
    "node_modules",
    "typings/global",
    "typings/global.d.ts"
  ],
  "compileOnSave": true,
  "atom": {
    "rewriteTsconfig": false
  },
  "scripts": {
    "prepublish": "tsc"
  }
}

1 个答案:

答案 0 :(得分:6)

对于您的ng2-orm库,请尝试将typings条目package.json指向index.d.ts,然后重新导出所有内容,例如。

"typings": "./dist/definitions/index.d.ts"