如何在 Laravel (NPM) 中使用 JQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45393343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use JQuery in Laravel (NPM)
提问by HansMuff
Im new to web development and I work with the Laravel framework. I use npm for handling with packages. But now I have a problem to implement JQuery. Bootstrap is implemented to larval and it works
我是 Web 开发的新手,我使用 Laravel 框架。我使用 npm 来处理包。但是现在我在实现 JQuery 时遇到了问题。Bootstrap 被实现到幼虫并且它可以工作
<link rel="stylesheet" href="/css/app.css">
In my Laravel project is in the package.json
在我的 Laravel 项目中是 package.json
"devDependencies": {
"axios": "^0.15.3",
"bootstrap-sass": "^3.3.7",
"cross-env": "^3.2.3",
"jquery": "^3.1.1",
"laravel-mix": "0.*",
"lodash": "^4.17.4",
"vue": "^2.1.10",
"webpack": "2.2.1"
}
Does that mean that Jquery is already in my project? But how looks the link like above from Bootstrap for jquery. For Bootstrap I have to compile new code (npm run dev) have I to do the same for Jquery?
这是否意味着 Jquery 已经在我的项目中?但是如何从 Bootstrap for jquery 看起来像上面的链接。对于 Bootstrap,我必须编译新代码(npm run dev),我是否也必须为 Jquery 做同样的事情?
回答by prola
JQuery is already installed within your project. To use it place the reference within your view.
JQuery 已安装在您的项目中。要使用它,请将参考放置在您的视图中。
<script src="{{ mix('js/app.js') }}"></script>
and run npm run dev
并运行 npm run dev
回答by Ikbel
Make sure your resources/assets/js/bootstrap.js
got jQuery imported:
确保您已resources/assets/js/bootstrap.js
导入 jQuery:
window.$ = window.jQuery = require('jquery')
Then you'll be able to use jQuery in your project.
然后你就可以在你的项目中使用 jQuery。
回答by Jayakusuma
all the submitted answer are correct, but i want to add that you need to put reference for your js/app.js as that is where your javascript and jquery file is and you need it to make it work. MORE importantly, you need to write it in every views that requires spesific javascript/jquery function
所有提交的答案都是正确的,但我想补充一点,您需要为您的 js/app.js 提供参考,因为这是您的 javascript 和 jquery 文件所在的位置,您需要它才能使其工作。更重要的是,您需要在需要特定 javascript/jquery 函数的每个视图中编写它
<script src="{{ asset('js/app.js') }}"></script>