【问题标题】:Importing external JS Library to Angular 2 CLI @NgModule将外部 JS 库导入 Angular 2 CLI @NgModule
【发布时间】:2017-02-27 03:34:27
【问题描述】:

所以我一直在使用这个article 通过 Angular CLI 将 Dropzone.js 导入到我的 Angular 2 项目中。虽然这篇文章是在这个问题之前 2 个月前发表的,但我注意到这个人并没有在 @NgModule 中使用新的 Angular CLI 语法。

有没有办法可以将此Dropzone.js npm module 导入到我的项目中?

我已经设法将 Dropzone.js 文件包含在我的 angular-cli.json 中的“脚本”属性中:

"scripts": [
        "../node_modules/dropzone/dist/dropzone.js"
      ],

这是我的 dropzone 组件:

import {
    Component,
    OnInit,
    Input,
    Output,
    EventEmitter,
    ElementRef
} from '@angular/core';
import {
    Http
} from '@angular/http';

@Component({
    selector: 'dropzone',
    templateUrl: './dropzone.component.html',
    styleUrls: ['./dropzone.component.scss']
})
export class DropzoneComponent implements OnInit {

    private _dropzone: Dropzone;

    @Input()
    element: string = ".dropzoneArea";

    @Input()
    url: string = "file/post";

    @Input()
    uploadMultiple: boolean = false;

    @Input()
    maxFileSize: number = 2048;

    @Input()
    clickable: string = ".dropzoneArea";

    @Input()
    autoProcessQueue: boolean = true;

    @Input()
    addRemoveLinks: boolean = false;

    @Input()
    createImageThumbnails: boolean = false;

    @Input()
    previewTemplate: string = "<div style='display:none'></div>";

    @Input()
    acceptedFiles: string = "*";

    @Output()
    sending: EventEmitter < boolean > ;

    @Output()
    uploadprogress: EventEmitter < number > ;

    @Output()
    success: EventEmitter < any > ;

    @Output()
    error: EventEmitter < any > ;

    constructor(private _eleRef: ElementRef, private _http: Http) {

        this.sending = new EventEmitter < boolean > ();
        this.uploadprogress = new EventEmitter < number > ();
        this.success = new EventEmitter < any > ();
        this.error = new EventEmitter < any > ();

    }

    initDropzone() {

        this._http.get("api/getCsrf").subscribe(data => {

            let body = data.json();

            this._dropzone = new Dropzone(this.element, {
                url: this.url,
                uploadMultiple: this.uploadMultiple,
                maxFilesize: this.maxFileSize,
                clickable: this.clickable,
                autoProcessQueue: this.autoProcessQueue,
                addRemoveLinks: this.addRemoveLinks,
                createImageThumbnails: this.createImageThumbnails,
                previewTemplate: this.previewTemplate,
                acceptedFiles: this.acceptedFiles,
                params: {
                    _csrf: body._csrf
                }
            });

            this._dropzone.on("sending", (file, xhr, formaData) => {

                this.sending.emit(true);

            });

            this._dropzone.on("uploadprogress", (file, progress, bytesSent) => {

                this.uploadprogress.emit(progress);

            });

            this._dropzone.on("success", (file) => {

                this.success.emit(file);

            });

            this._dropzone.on("error", (file, message) => {

                this.error.emit(message);

            });

        });

    }

    ngOnInit() {

        this.initDropzone();

    }
}

这是我在加载应用程序时遇到的错误

client?93b6:76 [default] /projects/project/dropzone/dropzone.component.ts:79:33 
Cannot find name 'Dropzone'.

有人对此有任何想法吗?我实际上可以访问 Dropzone 对象,因为它已附加到 Window,但我无法让我的组件找到它。

【问题讨论】:

    标签: angular dropzone.js angular-cli


    【解决方案1】:

    TypeScript 对 JS 文件一无所知,所以如果你有一些全局性的东西,你仍然需要以一种或另一种方式告诉 TypeScipt。最简单的方法是声明一个任意变量

    declare var Dropzone: any
    

    这足以编译代码,因为 TypeScript 只是在寻找名称 Dropzone。因为我们把它输入到any。它并不真正关心我们之后如何处理该符号。它只是接受我们知道我们在用它做什么。

    当使用带有 TypeScript 的 JS 库时,最好使用 TypeScript 定义文件。如果库不支持 TypeScript(在库中没有包含它自己的定义文件),对于流行的库,很可能已经有一个定义文件。 Dropzone 是那些流行的库之一。

    npm install --save-dev @types/dropzone
    

    现在您可以在任何需要的地方导入它

    import Dropzone from 'dropzone'
    

    现在你应该得到强类型和智能感知。请注意,对我来说,使用 VSCode,我必须重新启动 IDE 才能启动智能感知。这不是必需的,它可能是 VSCode 错误。

    【讨论】:

    • 嘿@peeskillet,感谢您的回答。当我安装类型时,我尝试在我的 Dropzone 组件中 import Dropzone from 'dropzone' in @NgModule and it says the module can't be found. When I try declare var Dropzone: any`,它消除了 Dropzone 错误之一,但我仍然得到这个:main.bundle.js:58546 [default] /Applications/MAMP/htdocs/ll-client-web/src/app/dropzone/dropzone.component.ts:13:23 Cannot find name 'Dropzone'.
    • 您可能需要将全局 TypeScript 更新到最新版本。 @types 支持直到 TypeScript 2.0 才添加。或者您的 IDE 可能还不支持 @types。尝试从命令行构建它,看看它是否不仅仅是一个 IDE 问题
    • 恐怕我不能 100% 确定您的意思。如何查看我拥有的 Typscript 版本?我该怎么做才能从命令行构建它?
    • 检查只是做tsc -v。 tsc 是 typescript 的 npm 安装附带的 typescript 编译器。如果它早于 2.0,只需安装一个新的 npm install typescript -g.. 我的意思是构建你的项目。如果这只是一个 IDE 问题,那么项目应该可以正常构建,除非您安装了旧版本的 typescript。要构建它,您只需 ng build 或 ng serve
    • 好吧,我明白了。我还需要重新启动项目。德普。非常感谢@peeskillet,你太棒了:D
    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2016-09-02
    • 2020-04-05
    • 1970-01-01
    • 2017-06-15
    • 2017-04-21
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多