如何在Angular 2组件中使用jQuery插件?

时间:2017-04-19 09:29:27

标签: jquery angular

我想在Angular 2应用程序的主页上使用名为Vegas的背景图片滑块插件。 jQuery插件已经通过npm安装在/ node_module目录下。

这是我的主页组件:

import { Component, OnInit, ElementRef } from '@angular/core';
import { Router } from '@angular/router';
import { Title } from '@angular/platform-browser';
import $ from 'jquery';
import 'vegas';

@Component({
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

    constructor(private el: ElementRef, router:Router, title:Title) {
        router.events.subscribe((url)=>{
            title.setTitle('Home Page');
        });
    }

    ngOnInit() {
        $('body').vegas({
            slides: [
                { src: "../../img/home/1.jpg" },
                { src: "../../img/home/2.jpg" },
                { src: "../../img/home/3.jpg" },
                { src: "../../img/home/4.jpg" }
            ]
        });
    }
}

我有这个错误:

  

未捕获(承诺):错误:错误:0:0引起:   __WEBPACK_IMPORTED_MODULE_3_jquery ___默认(...)(...)。拉斯加斯不是一个函数......

似乎没有加载插件文件。如何正确加载和使用jQuery插件?

1 个答案:

答案 0 :(得分:0)

你应该给这样的路径。

v-if