[英]Angular 7 - jQuery Plugin, can't find function
I'm currently trying to use a jQuery plugin, called cube portfolio . 我目前正在尝试使用一个名为cube portfolio的jQuery插件。
So far I've added the plugins directory to my assets folder, added its JS and CSS to angular.json
, and tried the following: 到目前为止,我已将plugins目录添加到我的assets文件夹,将其JS和CSS添加到
angular.json
,并尝试以下操作:
import { Component, OnInit } from '@angular/core';
import * as AOS from 'aos';
import * as $ from 'jquery';
import * as cubeportfolio from 'src/assets/cubeportfolio/js/jquery.cubeportfolio.js';
@Component({
selector: 'app-all',
templateUrl: './all.component.html',
styleUrls: ['./all.component.scss']
})
export class AllComponent implements OnInit {
constructor() { }
ngOnInit() {
AOS.init();
$('#js-grid-masonry').cubeportfolio({
filters: '#js-filters-masonry',
layoutMode: 'grid',
defaultFilter: '*',
animationType: 'slideDelay',
gapHorizontal: 20,
gapVertical: 20,
gridAdjustment: 'responsive',
mediaQueries: [{
width: 1500,
cols: 5,
}, {
width: 1100,
cols: 4,
}, {
width: 800,
cols: 3,
}, {
width: 480,
cols: 2,
options: {
caption: '',
gapHorizontal: 10,
gapVertical: 10,
}
}],
caption: 'overlayBottomAlong',
displayType: 'bottomToTop',
displayTypeSpeed: 100,
// lightbox
lightboxDelegate: '.cbp-lightbox',
lightboxGallery: true,
lightboxTitleSrc: 'data-title',
lightboxCounter: '<div class="cbp-popup-lightbox-counter">{{current}} of {{total}}</div>',
});
}
}
Somehow the JS doesn't seem to work - following error show up in the console: 不知怎的,JS似乎不起作用 - 在控制台中显示错误:
ERROR TypeError: jquery__WEBPACK_IMPORTED_MODULE_3__(...).cubeportfolio is not a function.
ERROR TypeError:jquery__WEBPACK_IMPORTED_MODULE_3 __(...)。cubeportfolio不是函数。
Further code: 进一步代码:
"styles": [
"src/assets/cubeportfolio/css/cubeportfolio.min.css",
"src/styles.scss"
],
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"src/assets/cubeportfolio/js/jquery.cubeportfolio.min.js"
]
If your are really determine to introduce the misery of jQuery use ngAfterViewInit, not ngOnInit as the DOM doesn't exist yet. 如果您真的决定引入jQuery的痛苦使用ngAfterViewInit,而不是ngOnInit,因为DOM还不存在。
Get rid of 摆脱
import * as cubeportfolio from 'src/assets/cubeportfolio/js/jquery.cubeportfolio.js';
as importing jQuery plugin scripts extend the jQuery object and should not be imported. 因为导入jQuery插件脚本扩展了jQuery对象,不应该导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.