javascript - 在 Angular 2 中使用普通 js 代码 (angular-cli)

标签 javascript angular angular2-directives angular2-modules

我正在尝试实现 Vanilla Tilt JS plugin 在我的项目中,但我什至似乎找不到在我的项目中使用 vanilla js 导入的方法。

到目前为止,我已经尝试将它用作指令,就像您通过使用来自 @angular 的 ElementRefInput 等来使用 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/

相关文章:

angular - 使用 Angular Directive(指令)向宿主元素添加按钮

javascript - 我如何在悬停时更改图像源并单击 Angular 2 其在 ngFor 生成的列表中

javascript - 如何从 From 外部更改 Formik TextField 值?

javascript - 为什么 for...of 语句中的三元运算符不起作用?

javascript - TS2307 : Cannot find module './images/logo.png'

node.js - Angular 6 中的 socket.io-stream 导入问题

angular - Angular 输入[类型=数字]中的验证错误?

angular2 指令 `Cannot read property ' 使用输出元数据订阅“未定义”

javascript - 使用node.js从url下载文件到服务器

angular - 将提供者注入(inject)另一个提供者