, поэтому я использовал этот article, чтобы получить Dropzone.js, импортированный в мой проект Angular 2 через Angular CLI. Хотя эта статья была опубликована чуть более 2 месяцев назад перед этим вопросом, я замечаю, что этот человек не использовал новый синтаксис Angular CLI с @NgModule.Импорт внешней библиотеки JS в Angular 2 CLI @NgModule
Есть ли способ импортировать этот Dropzone.js npm module в свой проект?
Я сумел включить в Dropzone.js файл в мои "сценарии" свойство в угловой-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, поскольку он прикреплен к окну, но я не могу заставить его компонент найти его.
Привет, @peeskillet, спасибо за ответ. Когда я установил типизацию, я попытался «импортировать Dropzone из« dropzone »в @NgModule, и он говорит, что модуль не может быть найден. Когда я пытаюсь «объявить var Dropzone: any» внутри моего компонента Dropzone, он избавляется от одной из ошибок Dropzone, но я все еще получаю этот код: 'main.bundle.js: 58546 [default]/Applications/MAMP /htdocs/ll-client-web/src/app/dropzone/dropzone.component.ts:13:23 Невозможно найти имя «Dropzone». –
Возможно, вам потребуется обновить глобальный TypeScript до последней версии. Поддержка '@ types' не добавляется до TypeScript 2.0. Или, может быть, ваша среда IDE еще не поддерживает '@ types'. Постарайтесь построить его из командной строки и посмотреть, не проблема ли это в IDE –
Боюсь, я не уверен на 100%, что вы имеете в виду. Как я могу проверить, какая версия Tyepscript у меня есть? Что я могу сделать, чтобы создать его из командной строки? –