how to add jquery in laravel project

前端 未结 5 680
忘了有多久
忘了有多久 2020-12-09 08:11

I am new to laravel framework. I want to use jQuery in web application built using laravel framework.

But don\'t know how to link to jQuery library

5条回答
  •  悲&欢浪女
    2020-12-09 09:07

    To add jquery to laravel you first have to add the Scaffolded javascript file, app.js

    This can easily be done adding this tag.

    
    

    There are two main procesess depending on the version but at the end of both you will have to execute:

    npm run dev
    

    That adds all the dependencies to the app.js file. But if you want this process to be done automatically for you, you can also run:

    npm run watch
    

    Wich will keep watching for changes and add them.

    Version 5.*

    jQuery is already included in this version of laravel as a dev dependency.

    You just have to run:

    npm install
    

    This first command will install the dependencies. jquery will be added.

    Version 6.* and 7*

    jQuery has been taken out of laravel that means you need to import it manually.

    I'll import it here as a development dependency:

    npm i -D jquery
    

    Then add it to the bootstrap.js file in resources/js/bootstrap.js You may notice that axios and lodash are imported there as well so in the same way we can import jquery.

    Just add wherever you want there:

    //In resources/js/bootstrap.js
    window.$ = require('jquery');
    

    If you follow this process in the 5.* version it won't affect laravel buy it will install a more updated version of jquery.

提交回复
热议问题