我将这些模块导入我的angular2组件时遇到问题。我使用AngularClass的angular2-webpack-starter。
我用npm:
安装dependeciesnpm install jquery --save
npm install malihu-custom-scrollbar-plugin --save
并安装打字:
typings install jquery --save --ambient
typings install mcustomscrollbar --save --ambient
我想在这样的组件中使用它:
jQuery('.selector').mCustomScrollbar();
这样做的最佳解决方案是什么?
我尝试使用此解决方案:Whats the best way to use jquery widgets inside angular 2?
但它不起作用,我得到错误“jQuery没有定义”。
答案 0 :(得分:8)
是强>
Angular:2.0.0
打字稿:2.0.2
Angular-cli:1.0.0-beta.16
webpack:2.1.0-beta.22
节点:4.6
npm:2.15.9
<强> ANSWER 强>
将类型添加到src / tsconfig.json
<强>解强>
获取所需的包裹
npm install jquery malihu-custom-scrollbar-plugin --save
npm install @types/jquery @types/jquery-mousewheel @types/mcustomscrollbar --save-dev
将插件css和脚本添加到根文件夹中的angular-cli.json
//angular-cli.json
"apps": [
{
...,
"styles": [
...,
"../node_modules/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.js",
"../node_modules/malihu-custom-scrollbar-plugin/node_modules/jquery-mousewheel/jquery.mousewheel.js",
"../node_modules/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.concat.min.js"
],
...
}
]
将类型添加到src / tsconfig.json
//tsconfig.json
{
"compilerOptions": {
...,
"typeRoots": [
"../node_modules/@types/"
],
"types": [
"jquery","jquery-mousewheel","mCustomScrollbar"
],
...
}
}
制定指令
//scrollbar.directive.ts
import {Directive, ElementRef, OnInit} from '@angular/core';
declare var $:any; //<-- let typescript compiler know your referencing a var that was already declared
@Directive({
selector: 'scrollbar',
host: {'class':'mCustomScrollbar'}, //<-- Make sure you add the class
})
export class ScrollbarDirective implements OnInit {
el: ElementRef;
constructor(el:ElementRef) {
this.el = el;
}
ngOnInit() {
//$(function(){ console.log('Hello'); }); <--TEST JQUERY
//check if your plugins are loading
//$().mousewheel) ? console.log('mousewheel loaded') : console.log('mousewheel not loaded');
//$().mCustomScrollbar) ? console.log('mCustomScrollbar loaded') : console.log('mCustomScrollbar not loaded');
$(this.el.nativeElement).mCustomScrollbar({
autoHideScrollbar: false,
theme: "light",
advanced: {
updateOnContentResize: true
}
});
在ngModule中包含指令并应用于组件的模板
//app.module.ts
import {ScrollbarDirective} from "./shared/UI/scrollbar.directive";
import {AppComponent} from "./app.component";
@NgModule({
...
declarations: [
AppComponent,
...,
ScrollbarDirective
],
...
})
export class AppModule{}
//app.component.ts
@Component({
selector: 'dashboard',
templateUrl: `
<scrollbar>
<div *ngFor="let thing of things">thing</div><br/>
</scrollbar>
`
})
答案 1 :(得分:0)
您需要在页面中的某处包含JQuery和插件:
答案 2 :(得分:0)
您必须在index.html页面中通过script标记包含jquery / plugin。
假设您使用的是TypeScript,您可以从组件中引用jquery,如下所示:
import {Component, ElementRef, OnInit} from 'angular2/core';
declare var jQuery:any;
@Component({
selector: 'jquery-integration',
templateUrl: './components/jquery-integration/jquery-integration.html'
})
export class JqueryIntegration implements OnInit {
elementRef: ElementRef;
constructor(elementRef: ElementRef) {
this.elementRef = elementRef;
}
ngOnInit() {
jQuery(this.elementRef.nativeElement).mCustomScrollbar();
}
}
此处有更多信息:http://www.syntaxsuccess.com/viewarticle/using-jquery-with-angular-2.0