gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:02:04 25 4
gpt4 key购买 nike

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

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

我有这样的配置:

打字.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)'
]
});
};

系统配置.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' }
};

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

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?

提前致谢。

编辑

与:从“hellojs”导入 {hello}从“hellojs”导入 helloimport 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。

最佳答案

终于解决了我的问题:

在 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" />

我的问题似乎与打字的最新更新有关,其中“ambient”被替换为“global”作为此依赖项的前缀。

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

关于typescript - 使用 Angular2 中的 globalDependencies(Typescript tsd 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37579995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com