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

问题描述 投票:7回答:1

问题

当我从'ng2-orm'导入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 <[email protected]>",
  "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"
  }
}
angular typescript ionic2
1个回答
6
投票

对于你的ng2-orm库,尝试将typingspackage.json条目指向index.d.ts,它会重新输出所有内容,例如:

"typings": "./dist/definitions/index.d.ts"
© www.soinside.com 2019 - 2024. All rights reserved.