【问题标题】:How to use a jQuery plugin in an Angular 2 component?如何在 Angular 2 组件中使用 jQuery 插件?
【发布时间】:2017-09-15 10:38:09
【问题描述】:

我想在我的 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___default(...)(...).vegas 不是函数...

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

【问题讨论】:

  • 你从哪里导入vegas?不用指定吗?
  • @DarrenSweeney 我是 Angular 2 的新手。安装 npm 后我 import 'vegas'; 时它不会加载所有内容吗?或者我应该导入 javascript 文件的整个路径?像这样:import '../../node_modules/vegas/dist/vegas.js';?
  • 我不熟悉 Angular,但规范是告诉模块从哪里导入 - 路径

标签: jquery angular


【解决方案1】:

你应该给出这样的路径。

Import 'vegas' from 'vegas/dist/vegas';

【讨论】:

    猜你喜欢
    • 2016-05-16
    • 2018-02-12
    • 2019-12-16
    • 2017-10-11
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    相关资源
    最近更新 更多