我试图向我的表中添加tablesorter,但最后在浏览器控制台上出现了以下错误。Uncaught TypeError: n(...).tablesorter is not a function,这是我的application.js
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "@rails/ujs"
import * as ActiveStorage from "@rails/activestorage"
require('jquery')
import "channels"
import "cocoon";
import "chartkick/chart.js"
import tablesorter from 'tablesorter/dist/js/jquery.tablesorter'
Rails.start()
ActiveStorage.start()
import 'bootstrap/dist/js/bootstrap'
import 'bootstrap/dist/css/bootstrap'
import '../stylesheets/application'
import "@fortawesome/fontawesome-free/js/all"
require("stylesheets/application")
$(function () {
$('.tablesorter').tablesorter({
showProcessing: true,
// initialize zebra and filter widgets
widgets: ["zebra", "filter"],
headers: {
0: { sorter: false, filter: false },
13: { sorter: true, filter: true }
}
});
})我在Gemfile jquery-tablesorter中添加了gem。我也做过yarn add jquery和yarn add tablesorter,但是我仍然被困住了。
注意:jquery-tablesorter是我在Gemfile中拥有的唯一与jquery相关的gem。
发布于 2022-03-29 07:12:41
我不知道$是在哪里装载的。Tablesorter没有权限定义tablesorter函数。您需要显式导入它。
import jQuery from "jquery";
import "tablesorter/dist/js/jquery.tablesorter";
window.$ = window.jQuery = jQuery;或
window.$ = window.jQuery = require("jquery");
require("tablesorter/dist/js/jquery.tablesorter");https://stackoverflow.com/questions/71647298
复制相似问题