如何在Rails 6 Webpacker中添加jquery第三方插件 [英] How to add jquery third party plugin in rails 6 webpacker

查看:95
本文介绍了如何在Rails 6 Webpacker中添加jquery第三方插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它很简单,但是对rails 6进行了更新.rails6中有新的语法来管理由webpacker维护的javascript资产.

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
require("@rails/ujs") //.start()
require("turbolinks").start()
require("@rails/activestorage").start()
require('jquery').start()
require('jquery_ujs').start()
require('bootstrap-daterangepicker').start()
require("custom/custom").start()
require("bootstrap").start()
require("channels")

我能够添加custom/custom,但是引导程序和jquery无法正常工作 我已经通过npm安装了jquery和bootstrap

i am able to add custom/custom but bootstrap and jquery is not working i have install jquery and bootstrap via npm

推荐答案

在命令下方运行以添加jQuery.

run below command to add jQuery.

$ yarn add jquery

config/webpack/environment.js

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

在application.js文件中需要jquery.

Require jquery in application.js file.

require('jquery')

无需再添加jquery-rails gem!

No more need to add jquery-rails gem!

这篇关于如何在Rails 6 Webpacker中添加jquery第三方插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆