angular - 将 vanilla-tilt.js 添加到 Angular 6 组件(Angular 组件中的外部 js 库)
问题描述
您好,我想在我的一个角度组件中使用名为 vanilla-tilt.js 的第三方 js 库。这是图书馆的链接:
https://micku7zu.github.io/vanilla-tilt.js/
到目前为止我所做的是:使用 npm 安装并将其链接到我的 angular.json 文件中,如下所示:
"scripts": [
"node_modules/vanilla-tilt/dist/vanilla-tilt.js"
]
然后在我的 component.ts 中我做了以下事情(我只提供了必要的代码并且'.about-pic'
是<img>
我在我的 html 文件中选择的):
import { VanillaTilt } from 'vanilla-tilt/dist/vanilla-tilt.js';
export class AboutComponent implements OnInit, AfterViewInit {
ngAfterViewInit() {
VanillaTilt.init(document.querySelector('.about-pic'), {
max: 25,
speed: 400
});
}
}
我ngAfterViewInit()
从上面链接的网站中获取了代码,在“JS Way”下
我以为我正确导入了这个外部库,但我在控制台中收到以下错误:
ERROR TypeError: Cannot read property 'init' of undefined
我想我不太了解在 Angular 组件中安装第三方 js 库的概念。
任何人都可以帮忙吗?
谢谢!
解决方案
按照https://micku7zu.github.io/vanilla-tilt.js/ JS 方式,我在 ngOnInit 中尝试了同样的方法。它对我有用。
- npm install vanilla-tilt
- 在 ts 文件中声明 var VanillaTilt
ngOnInit() {
VanillaTilt.init(document.querySelector(".tilt-image"), { max: 25, speed: 400 });
VanillaTilt.init(document.querySelectorAll(".tilt-image"));
}
- 在 html
div 类="倾斜倾斜图像" 数据倾斜数据倾斜眩光="true" 数据倾斜规模="1.1"
img src="assets/images/sample.png"
/div
注意:“倾斜图像”是你的元素
推荐阅读
- spring - Spring HATEOAS:请求参数在链接中显示为路径变量
- reactjs - A/B 测试在谷歌中添加元素优化渲染两次(React/Nextjs 应用程序)
- prolog - 如何在 ECLiPSe 中读取大型 Prolog 文件?
- centos - Changing the wildfly.service to newname_wildfly.service
- python - 如何对集合列表求和?在一个类中使用字符串和整数?
- python - 如何在包含具有字典结构的 dynamodb 表的数据框中访问字典的值?
- database - 如果主设备的 GTID 与从设备的 GTID 不同,我怎么能惊慌失措?
- reactjs - 在样式化组件上转发 InnerRef 会导致打字稿错误
- python - 使用python引发TypeError(“选项值必须是字符串”)
- php - 访问配置流明,读取文件?