首页 > 解决方案 > 使用 webpack 获取 'Uncaught TypeError: $(...).tablesorter is not a function'

问题描述

Uncaught TypeError: $(...).tablesorter is not a function在使用 Webpack 迁移到 Rails 6 后,我得到了。这是我的 environment.js

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery',
    tablesorter: 'tablesorter',
    bootstrap: 'bootstrap/dist/js/bootstrap',
    draggable: 'plain-draggable'
  })
)

module.exports = environment

相关的其他插件工作正常。

标签: javascriptruby-on-railsnpmwebpackyarnpkg

解决方案


我不知道究竟是为什么,但它的工作原理是这样的:

import $ from 'jquery'
import 'tablesorter'

$(document).on('turbolinks:load', () => {
  $('.tablesorter').tablesorter()
})

推荐阅读