【问题标题】:Uncaught TypeError: (...).modal is not a function in ES6Uncaught TypeError: (...).modal 不是 ES6 中的函数
【发布时间】:2019-03-11 16:00:35
【问题描述】:

我需要另一双眼睛和知识,因为我是 ES6 新手。

这是我尝试使用 fullcalendar.io 的 index.js 文件:

import $ from 'jquery';
import 'bootstrap';
import 'fullcalendar';
$(function() {
    $('#calendar').fullCalendar({
     // ... 

     eventClick: function(event, jsEvent, view) {

         $('#calendarEventModal').modal();
     }
    })     
})

问题是当我运行此代码并单击事件时,我收到以下错误:

Uncaught TypeError: (0 , _jquery2.default)(...).modal is not a function

我做错了什么?缺什么 ?

【问题讨论】:

  • 试试:import $ from 'jquery'; window.jQuery = window.$ = $; import 'bootstrap'; ...
  • @Andreas - 感谢您的提示,但我尝试过但没有成功:( - 仍然是同样的错误....
  • 这个问题可能是由多个 jQuery 导入引起的吗?主要模板是导入jQuery ....
  • 缺少来自bootstrap 的模式,一旦添加,请尝试清除缓存并检查。
  • 您能否更新问题以显示您当前的标记?

标签: javascript jquery twitter-bootstrap npm ecmascript-6


【解决方案1】:

至少有两种情况$('#calendarEventModal')没有modal()方法:

  • #calendarEventModal 在调用方法时与 DOM 中的元素不匹配(检查您是否可以使用:
if ($('#calendarEventModal').is('#calendarEventModal')) {
  console.log('i has found zee DOM element'); 
  $('#calendarEventModal').modal();
} else {
  console.log('no joy. `#calendarEventModal` missing in action...'); 
}

...显然,您不需要 console.log() )。
简单来说:您的 DOM 中是否有一个带有 id="calendarEventModal" 的元素,在调用 .modal() 时保存您想在模态中看到的任何内容?
它是否放置在内部 #calendar?如果是这样,也许你应该把它拿出来。我不知道.fullCalendar() 对元素执行什么操作,但它可能会用一些生成的标记替换它的innerHTML,从而破坏你的#calendarEventModal

  • 导入的'bootstrap' 包没有modal() 方法 - 可以创建不包括所有模块的自定义引导包。但是,它是默认包含在内的,因此,如果您没有精简包装,您很可能会遇到第一种情况。

【讨论】:

  • 您好,感谢您的提示!我已经尝试过,并且确实在 $('#calendarEventModal') 中有正确的 DOM 元素。我将尝试检查 bootstrap 是否具有 modal() 方法,但我看不出它不应该的原因?!
猜你喜欢
  • 2020-04-29
  • 2019-11-06
  • 2015-11-26
  • 2020-09-10
  • 1970-01-01
  • 2015-05-09
  • 2017-09-18
  • 2020-06-05
  • 1970-01-01
相关资源
最近更新 更多