elixir - Phoenix Framework - jQuery connection
问题描述
I have problem with jquery connection to my Project on the Phoenix Framework. I have installed jQuery through npm install
npm i jquery
I add import to app.js.
import $ from 'jquery'
My dependencies at package.json
"jquery": "^3.4.1",
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html"
But in console I have error
Uncaught ReferenceError: $ is not defined
My js
<script>
$("#login-btn").click(function(){
console.log("Sign in button pressed");
});
</script>
Please, help me.
解决方案
您需要将此行添加到 assets/webpack.config.js
const webpack = require('webpack');
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
})
]
和 assets/js/app.js
import $ from 'jquery'
window.jQuery = $
window.$ = $
推荐阅读
- java - 我们可以为 Kafka Producer 分配源端口吗?
- python - 类型错误:edit_undo() 接受 1 个位置参数,但给出了 2 个
- java - Java 14 中的 Lambda 表达式和“?:”运算符
- python - Pygame Sprite 在移动时会留下痕迹
- javascript - 使用 ES lint 错误希望在不禁用它或更改规则的情况下解决
- azure - azure webapp 您无权查看此目录或页面
- amazon-web-services - AWS SES 允许跟踪邮件是否发送或失败
- python - CI / CD管道的Postgresql数据库错误
- reactjs - 从反应上传图像到laravel api
- c - 您如何理解驻留在特定堆栈/函数中的局部变量地址的差异?