how to add jquery in laravel project

前端 未结 5 667
忘了有多久
忘了有多久 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 08:46

    For those using npm to install packages, you can install jquery via npm install jquery and then use elixir to compile jquery and your other npm packages into one file (e.g. vendor.js). Here's a sample gulpfile.js

    var elixir = require('laravel-elixir');
    
    elixir(function(mix) {
        mix
    
        .scripts([
            'jquery/dist/jquery.min.js',
            // list your other npm packages here
        ],
        'public/js/vendor.js', // 2nd param is the output file
        'node_modules')        // 3rd param is saying "look in /node_modules/ for these scripts"
    
        .scripts([
            'scripts.js'       // your custom js file located in default location: /resources/assets/js/
        ], 'public/js/app.js') // looks in default location since there's no 3rd param
    
        .version([             // optionally append versioning string to filename
            'js/vendor.js',    // compiled files will be in /public/build/js/
            'js/app.js'
        ]);
    
    });
    
    0 讨论(0)
  • 2020-12-09 08:51

    You can link libraries from cdn (Content delivery network):

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    

    Or link libraries locally, add css files in the css folder and jquery in js folder. You have to keep both folders in the laravel public folder then you can link like below:

    <link rel="stylesheet" href="{{asset('css/bootstrap-theme.min.css')}}">
    <script src="{{asset('js/jquery.min.js')}}"></script>
    

    or else

    {{ HTML::style('css/style.css') }}
    {{ HTML::script('js/functions.js') }}
    

    If you link js files and css files locally (like in the last two examples) you need to add js and css files to the js and css folders which are in public\js or public\css not in resources\assets.

    0 讨论(0)
  • 2020-12-09 08:59

    you can use online library

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    

    or else download library and add css in css folder and jquery in js folder.both folder you keep in laravel public folder then you can link like below

    <link rel="stylesheet" href="{{asset('css/bootstrap-theme.min.css')}}">
    <script src="{{asset('js/jquery.min.js')}}"></script>
    

    or else

    {{ HTML::style('css/style.css') }}
    {{ HTML::script('js/functions.js') }}
    
    0 讨论(0)
  • 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.

    <script src="{{ asset('js/app.js') }}"></script>
    

    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.

    0 讨论(0)
  • 2020-12-09 09:07

    In Laravel 6 you can get it like this:

    try {
        window.$ = window.jQuery = require('jquery');
    } catch (e) {}
    
    0 讨论(0)
提交回复
热议问题