具有第三部分库和 angular-cli 的 Intellisense

Intellisense with 3rd part libraries and angular-cli

在使用 angular2-cli 创建的项目中,我需要使用一些第 3 部分 javascript 库,这些库应该在全局 javascript 范围内可用(例如 TWEEN 命名空间来自tween.js 图书馆)。

根据 angular-cli guide 将 js 库安装为全局,我已经使用 npm 安装了它,并将库脚本添加到 angular-[= 的 "scripts" 数组中24=] 文件.

"scripts": [
    "../node_modules/tween.js/src/Tween.js"
  ],

为了在 angular 组件中使用全局 TWEEN 命名空间,我在文件开头将其声明为常量变量,如下所示:

import { Component, OnInit } from '@angular/core';

declare const TWEEN: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'app works!';

  constructor() { }

  ngOnInit() {

    let tween = new TWEEN.Tween({x: 0}).to({x: 1}, 2000);

    // .. 

  }
}

这可行,但问题是我没有通过这种方式获得任何第 3 部分库的智能感知(我使用的是 WebStorm)。我可以做些什么来让智能感知工作吗?或者是否有更好的方法在 angular 2 工作流程中导入第 3 部分 javascript 库?

鉴于您在文件顶部将 TWEEN 定义为 "any",因此您永远不会以这种方式获得智能感知,因此可以说它可以是任何东西(无类型,无智能感知).

你应该看看这个 post: https://weblog.west-wind.com/posts/2016/Sep/12/External-JavaScript-dependencies-in-Typescript-and-Angular-2
这几乎是在告诉您,您要么在库本身中有打字,当您安装它时它是免费的,要么只是解决没有打字的问题。

如果您要使用根本没有类型的库,您可能需要考虑自己创建类型。