我正在尝试实现 Vanilla Tilt JS plugin 在我的项目中,但我什至似乎找不到在我的项目中使用 vanilla js 导入的方法。
到目前为止,我已经尝试将它用作指令,就像您通过使用来自 @angular 的
。ElementRef
、 Input
等来使用 jQuery 插件一样/核心
我已直接从 npm package 将脚本包含在 scripts:
下的 .angular-cli.json
文件中。 我已经为 Vanilla Tilt JS 安装了。
我正在考虑将它用作指令,因为它有自己的 data-tilt
属性,如下所示:
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[data-tilt]'
})
export class ParallaxDirective {
constructor(private el: ElementRef) {
}
}
但尽管我到目前为止已经做出了努力,但我找不到办法做到这一点。我是 Angular 2 的新手。
我的问题是:
是否可以在 Angular 2 中使用普通的 javasciprt 插件,如果可能,如何实现 Vanilla-Tilt JS 插件?
最佳答案
您应该能够导入 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
});
}
}
关于javascript - 在 Angular 2 中使用普通 js 代码 (angular-cli),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42536186/