tablesorter 不是函数 rails 6 错误

tablesorter is not a function rails 6 error

我正在尝试将 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 jqueryyarn add tablesorter 但我仍然卡住了。

注意:jquery-tablesorter 是我的 Gemfile 中唯一与 jquery 相关的 gem。

我不确定 $ 被加载到哪里。 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");