2017-03-01 31 views
0

我試圖在我的項目中實現Vanilla Tilt JS plugin,但我似乎甚至找不到在我的項目中使用vanilla js import的方法。在Angular 2中使用vanilla js代碼(angular-cli)

到目前爲止,我已經嘗試使用它作爲指令,就像你會使用ElementRef,Input等從@angular/core等jQuery插件。

我已經scripts:直接從我已經安裝了香草傾斜JS的npm package下列入我.angular-cli.json文件中的腳本。

我想使用它作爲一個指令,因爲它有自己的屬性data-tilt像這樣:

import { Directive, ElementRef, HostListener, Input } from '@angular/core'; 

@Directive({ 
    selector: '[data-tilt]' 
}) 
export class ParallaxDirective { 
    constructor(private el: ElementRef) { 

    } 
} 

但我不能找到一個方法來做到這一點,儘管我努力至今。我是新來的角2

我的問題是:

是否有可能使用普通javasciprt插件角2內,如果可能的話,我怎麼能實現香草傾斜JS插件?

+0

入住這http://stackoverflow.com/questions/35796961/angular-2-adding-3rd-party-libs –

回答

1

您應該能夠導入Vinilla-tilt並使用傳遞給構造函數的元素引用。

import { Directive, ElementRef, HostListener, Input } from '@angular/core'; 

const VanillaTilt = require('vanilla-tilt'); 

@Directive({ 
    selector: '[data-tilt]' 
}) 
export class ParallaxDirective { 
    constructor(private el: ElementRef) { 
     VanillaTilt.init(el.nativeElement, { 
     max: 25, 
     speed: 400 
     }); 
    } 
} 
+1

唯一缺少的是這裏的'el.nativeElement',使一個參考DOM。 – snkv

+1

當然,很好看! –

相關問題