angular - Usar globalDependencies (archivos Typescript tsd) de Angular2
Estoy intentando usar los tsd de TypeScript de DefinitelyTyped en un proyecto de Angular2 (RC.0), pero las dependencias globales no parecen cargarse correctamente:
typings install --save dt~hellojs --global --save
npm install --save hellojs
Tengo estas configuraciones:
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)'
]
});
};
configuración-del-sistema.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' }
};
pero, si uso la nueva dependencia de un módulo de Angular, así:
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({});
}
}
Estoy recibiendo errores de compilador:
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'.
Si intento importar el módulo con import * as hello from "hellojs" o import hello=require("hellojs"), el fallo persiste.
¿Cómo puedo usar una dependencia global de Angular2?
Gracias de antemano.
Editar
Con: import {hello} from "hellojs" o import hello from "hellojs" o import hello=require("hellojs")
Estoy recibiendo este error:
Error: Typescript found the following errors: /Users/fer2d2/dev/personal/web-projects/front-joinfinity/tmp/broccoli_type_script_compiler-input_base_path-tKuuBwei.tmp/0/src/app/+login/login.component.ts (7, 21): Cannot find module 'hellojs'.
Parece que Typescript o systemjs no están cargando correctamente las dependencias globales de tsd.
Respuesta #1
Finalmente resolví mi problema:
En Angular2, usando y utilizando Angular-CLI, tienes un archivo llamado typings.d.ts que importa todos los typings instalados mediante typings install. En este archivo puedes ver la siguiente línea de importación:
/// <reference path="../typings/browser.d.ts" />
El archivo browser.d.ts solo contiene las definiciones de tipos para Angular2 entre algunas otras definiciones. Si quieres usar globalDependencies, typings las agregará al archivo typings/index.d.ts. Por lo tanto, en tu typings.d.ts, debes agregar la siguiente línea:
/// <reference path="../typings/browser.d.ts" />
Mi problema parece estar relacionado con la última actualización de los typings, donde 'ambient' fue reemplazado por 'global' como prefijo para estas dependencias.
https://www.npmjs.com/package/typings#updating-from-0x-to-10
