如何在Laravel中使用/集成jQuery [英] How to use/integrate jquery in laravel
问题描述
任何想法如何处理?我的意思是我需要在项目中安装或管理一些文件吗?我不知道该怎么办?抱歉,我是laravel的新人
Any idea how to deal with that? I mean do I need to install or manage some files in my project? I don't know how to manage that? Sorry I am just new in laravel
推荐答案
我将分享两种方式
a)在您的视图中,在html head
a) In your view add this line in html head
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
并开始在script
标记中编写您的jquery代码
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
and start writing your jquery code in script
tag
b)我通常更喜欢的另一种方式是:
b) The other way which i generally prefer is :
在Laravel 5.6中,如果您查看您的package.json
文件jquery
已经作为npm依赖项存在,那么
In Laravel 5.6 if u look at your package.json
file jquery
is already present as npm dependency so
- 创建新的laravel项目
laravel new my-project
- cd到
my-project
并运行命令npm install
.这将安装package.json
中包括的所有依赖项,包括jquery
. - 现在要将所有这些依赖关系编译为一个文件,在
public/js/app.js
中,将行window.Jquery = require('jquery');
添加到您的resources/assets/js/app.js
-
运行命令
npm run dev
- create new laravel project
laravel new my-project
- cd to
my-project
and run commandnpm install
. This will install all dependencies included inpackage.json
includingjquery
. - now to compile down all these dependencies to one file in
public/js/app.js
add the linewindow.Jquery = require('jquery');
to yourresources/assets/js/app.js
run command
npm run dev
将行<script src="{{ asset('js/app.js') }}"></script>
添加到您的welcome.blade.php
现在将此代码添加到您的视图中.
add this code to your view now.
<script>
$(document).ready(function(){
$(".links").hide();
});
刷新页面.您现在将不会看到laravel下面的链接.这意味着jQuery已启动并正常工作
refresh page. you will not see the links below laravel now. which means jquery is up and working
这篇关于如何在Laravel中使用/集成jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!