How to add jquery third party plugin in rails 6 webpacker

后端 未结 4 669
滥情空心
滥情空心 2020-12-06 10:19

I know its simple but with update of rails 6. there is new syntax in rails 6 for manage javascript assets which is maintained by webpacker.

//application.js
         


        
4条回答
  •  攒了一身酷
    2020-12-06 10:24

    run below command to add jQuery.

    $ yarn add jquery
    

    Add below code in config/webpack/environment.js

    const webpack = require('webpack')
    environment.plugins.prepend('Provide',
      new webpack.ProvidePlugin({
        $: 'jquery/src/jquery',
        jQuery: 'jquery/src/jquery'
      })
    )
    

    Require jquery in application.js file.

    require('jquery')
    

    No more need to add jquery-rails gem!

提交回复
热议问题