使用Angular2中的globalDependencies(Typescript tsd文件)

时间:2016-06-01 22:46:41

标签: typescript angular

我正在尝试在Angular2项目(RC.0)中使用DefinitelyTyped中的typescript tsd,但是全局依赖项似乎没有正确加载:

typings install --save dt~hellojs --global --save
npm install --save hellojs

我有这样的配置:

typings.json:

"globalDependencies": {
  "hellojs": "registry:dt/hellojs#0.2.3+20160423145325"
}

angular.cli.build.js:

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      'angular2-moment/**/*.+(js|js.map)',
      'moment/**/*.+(js|js.map)',
      'hellojs/**/*.+(js|js.map)'
    ]
  });
};

系统config.ts

/** Map relative paths to URLs. */
const map: any = {
  'moment': 'vendor/moment',
  'angular2-moment': 'vendor/angular2-moment',
  'hellojs': 'vendor/hellojs/dist'
};

/** User packages configuration. */
const packages: any = {
  'moment': { main: 'moment.js', defaultExtension: 'js' },
  'angular2-moment': { main: 'index.js', defaultExtension: 'js' },
  'hellojs': { main: 'hello.all.js' }
};

但是,如果我使用角度模块中的新依赖项,如下所示:

import {Component} from "@angular/core";
import {FormBuilder, ControlGroup} from "@angular/common";
import {ROUTER_DIRECTIVES, Router} from "@angular/router";
import {LoginResourceService} from "../shared/services/resources/login-resource.service";
import {AuthService} from "../shared/services/auth.service";
import {AppRoutes} from "../app-routes";

@Component({
  moduleId: module.id,
  selector: 'app-login',
  templateUrl: 'login.component.html',
  directives: [ROUTER_DIRECTIVES],
  providers: [LoginResourceService],
  styleUrls: ['login.component.css']
})
export class LoginComponent {

  userForm:ControlGroup;
  errorMessage:string;

  constructor(formBuilder:FormBuilder, private _loginResource:LoginResourceService, public router:Router,
              private _auth:AuthService) {
    this.userForm = this.buildLoginForm(formBuilder);
  }
  // ...
  example() {
    hello.init({});
  }

}

我收到了编译错误:

Error: Typescript found the following errors:
  /Users/fer2d2/dev/personal/web-projects/front-joinfinity/tmp/broccoli_type_script_compiler-input_base_path-xa8whw1k.tmp/0/src/app/+login/login.component.ts (62, 5): Cannot find name 'hello'.

如果我尝试使用import * as hello from "hellojs"import hello = require("hellojs")导入模块,则失败仍然存在。

如何使用Angular2中的globalDependency?

提前致谢。

修改

使用: import {hello} from "hellojs"import hello from "hellojs"import hello = require("hellojs")

我收到了这个错误:

Error: Typescript found the following errors: /Users/fer2d2/dev/personal/web-projects/front-joinfinity/tmp/broccoli_type_scrip‌​t_compiler-input_base_path-tKuuBwei.tmp/0/src/app/+login/login.component.ts (7, 21): Cannot find module 'hellojs'.

似乎Typescript或systemjs没有正确地加载globalDependencies tsd。

1 个答案:

答案 0 :(得分:0)

最后我解决了我的问题:

在Angular2中,使用和使用Angular-CLI,您有一个名为typings.d.ts的文件,它导入通过typings install安装的所有类型。在此文件中,您可以看到以下导入行:

/// <reference path="../typings/browser.d.ts" />

browser.d.ts只包含Angular2在其他一些类型之间的输入。如果要使用globalDependencies,typings会将它们添加到文件typings / index.d.ts中。因此,在您的typings.d.ts中,您必须添加以下行:

/// <reference path="../typings/browser.d.ts" />

我的问题似乎与最新的打字更新有关,其中&#39; ambient&#39;被全球&#39;取代作为此依赖项的前缀。

https://www.npmjs.com/package/typings#updating-from-0x-to-10