【问题标题】:Angular 6 + jQuery + fullpage.jsAngular 6 + jQuery + fullpage.js
【发布时间】:2018-07-18 22:58:39
【问题描述】:

我正在尝试在没有 ngx-fullapge 的情况下使用 fullpage.js,但我遇到了一些与导入相关的问题:

首先,jquery 似乎不起作用,即使包已设置...

在一个非常简单的AppComponent 类定义中,我有:

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import * as fullpagejs from 'fullpage.js';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
})
export class AppComponent  implements OnInit {
  public items = [1, 2, 3];

  public ngOnInit() : void {
    $('#fullpage')
  }
}

使用非常简单的模板:

<div id="fullpage">

  <div class="section" id="landing">
    Landing
  </div>

  <div class="section" *ngFor="let item of items; let i = index" id="document_{{ i }}">
    Document {{ item }}
  </div>

  <div class="section" id="general-information">
    General Information
  </div>

</div>

angular.json 的脚本如下:

 "scripts": [
          "./node_modules/jquery/dist/jquery.min.js",
          "./node_modules/fullpage.js/dist/jquery.fullpage.min.js"
        ]

我有以下错误:

ERROR
Error: $ is not a function

我在这里创建了一个重现问题的在线项目:https://stackblitz.com/edit/angular-nrrujn

如何在这个项目中同时使用 jQuery 和 fullpage.js?

[编辑]

jQuery 可与 import $ from 'jquery'; 一起使用,但仍无法找到 fullpage.js 中的 fullpage() 方法。 我更新了stackblitz:https://stackblitz.com/edit/angular-nrrujn

【问题讨论】:

标签: javascript jquery angular fullpage.js


【解决方案1】:

确保jquery.fullpage.min.js 目录中存在'./node_modules/fullpage.js/dist/jquery.fullpage.min.js' 文件

【讨论】:

  • 欢迎来到 Stack Overflow!这篇文章似乎是您的第一个答案,它包含代码。请记住,您可以使用 markdown 语法添加代码块,这看起来要好得多。查看this help center article
【解决方案2】:

2018 年 11 月更新:

现在 fullPage.js 可用于 Angular: https://github.com/alvarotrigo/angular-fullpage


不是 Angular 方面的专家,但是...也许您应该在导入窗口对象后声明 $?

这个怎么样?

import { Component, OnInit } from '@angular/core';
import jQuery from 'jquery'; // <--- change here
import * as fullpagejs from 'fullpage.js';

if(!window.$){
    window.$ = jQuery;
}

【讨论】:

  • import jQuery as $ from 'jquery'; 是语法错误。重命名默认导出甚至没有意义。你给它起了个名字。
  • @LazarLjubenović 同意,这可以在stackblitz.com/edit/angular-nrrujn进行测试
【解决方案3】:

import * as $ from 'jquery'; 这行绝对正确。

确保 jquery 实际安装在您的项目中。运行npm install jquery@latest 进行安装。然后将其从scripts 数组中删除,因为它不属于那里。

【讨论】:

  • 试图重置为最新版本并删除脚本中的路径...但还是旧的 =/
  • @EhouarnPerret 尝试查看您的 package-lock.json 文件。 jquery在里面吗?它是正确的版本吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 2018-11-19
  • 2018-11-20
  • 1970-01-01
相关资源
最近更新 更多