gpt4 book ai didi

jquery - 如何将 jquery 和 mCustomScrollbar 插件导入 Angular2 组件

转载 作者:太空狗 更新时间:2023-10-29 18:06:19 26 4
gpt4 key购买 nike

我在将这些模块导入我的 angular2 组件时遇到问题。我使用 AngularClass 中的 angular2-webpack-starter。

我使用 npm 安装依赖项:

npm 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 is not defined”。

最佳答案

使用
Angular :2.0.0
typescript :2.0.2
Angular -cli: 1.0.0-beta.16
网络包:2.1.0-beta.22
节点:4.6
npm:2.15.9

回答
将类型添加到 src/tsconfig.json

解决方案

  1. 获取所需的包
    npm install jquery malihu-custom-scrollbar-plugin --save
    npm install @types/jquery @types/jquery-mousewheel @types/mcustomscrollbar --save-dev

  2. 将插件 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"
    ],
    ...
    }
    ]
  3. 将类型添加到 src/tsconfig.json

    //tsconfig.json
    {
    "compilerOptions": {
    ...,
    "typeRoots": [
    "../node_modules/@types/"
    ],
    "types": [
    "jquery","jquery-mousewheel","mCustomScrollbar"
    ],
    ...
    }
    }
  4. 制定指令

    //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
    }
    });
  5. 在 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>
    `
    })

关于jquery - 如何将 jquery 和 mCustomScrollbar 插件导入 Angular2 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36755625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com