LoginSignup
0
0

More than 1 year has passed since last update.

railsにjqueryを導入し、外部ファイルから読み込む時

Posted at

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'
})
)

app/javascript/packs/application.jsに下記を記述。
require("jquery")

これでscriptタグから作るjqueryの動作は作れる。

ただ外部ファイルに分けて動作をさせたい場合は、
app/javascriptの直下に任意のファイル(〇〇.js)を作り、
app/javascript/packs/application.jsに下記を記述。
require("〇〇")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0