【问题标题】:Intellisense with 3rd part libraries and angular-cli带有第 3 部分库和 angular-cli 的 Intellisense
【发布时间】:2017-01-25 03:42:33
【问题描述】:

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

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

"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 库?

【问题讨论】:

    标签: npm global intellisense angular2-cli


    【解决方案1】:

    您永远不会以这种方式获得智能感知,因为您将文件顶部的 TWEEN 定义为“任何”,因此说它可以是任何东西(没有类型,没有智能感知) .

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

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

    【讨论】:

    • 感谢您提供有用的链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    相关资源
    最近更新 更多