Dimitar
Dimitar

Reputation: 1928

How to import js libraries in laravel - blade template?

I want to import this library into my laravel project. I have run npm install vue-simple-lightbox and then in my blade template i have this code

@extends('layouts.app')

@section('content')
    {{-- some html code --}}
    <div class="row">
    </div>
    <div class="row">
        <lightbox id="mylightbox" :images="images" :image_class="'img-responsive img-rounded'" :album_class=" 'my-album-class' " :options="options"></lightbox>
    {{-- more html code --}}   
    </div>
@endsection

<style type="text/css">
    .dropdown, .dropdown-menu {
        width: 100%;
    }
</style>

@section('scripts')
    // import the library here?
    <script type="text/javascript">
        let app = new Vue({
            el: '#app',
            data() {
                return {
                    images : [
                        {
                            src : 'https://cdn.rawgit.com/vrajroham/vrajroham.github.io/85d64ac5/imgs/img1.jpg',
                            title : 'Image 2'
                        },
                        {
                            src : 'https://cdn.rawgit.com/vrajroham/vrajroham.github.io/85d64ac5/imgs/img2.jpg',
                            title : 'Image 3'
                        },
                        {
                            src : 'https://cdn.rawgit.com/vrajroham/vrajroham.github.io/85d64ac5/imgs/img3.jpg',
                            title : ''
                        },
                        {
                            src : 'https://cdn.rawgit.com/vrajroham/vrajroham.github.io/85d64ac5/imgs/img4.jpg',
                            title : ''
                        },
                      ],
                      options : {
                        closeText : 'X'
                      }
                };
            },
            mounted() {
            },
        });
    </script>
@endsection

Where should i import the library? I tried to import it into app.js file using this code window.Lightbox = require('vue-simple-lightbox');, but then how do i use it? It seems the blade template have no idea what is 'lightbox'.

I am getting this error

[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option.

What is the correct way of importing the library and then use it inside the blade template? Thanks!

Upvotes: 0

Views: 5138

Answers (2)

Marina Mosti
Marina Mosti

Reputation: 461

Extract your js code to a single file, blade templates are not compiled and it wont work if you import it there.

So copy everything over to say, app.js, then include it via a script tag

Inside app.js you can import lightbox from 'vue-simple-lightbox'

Now, make sure you add it to your webpack.mix file through

.js('path/to/app.js', 'public/js/app.js')

That way the library will get compiled into the asset.

Now, regarding the VUE tempalte not finding the lightbox component.
You forgot to add the component part of the Vue instance:

import Lightbox from 'vue-simple-lightbox'

export default {
components: {
  Lightbox //HERE
},
...

Upvotes: 1

Prince Lionel N&#39;zi
Prince Lionel N&#39;zi

Reputation: 2588

You can import the file directly from GitHub:

<script src="https://github.com/vrajroham/vue-simple-lightbox/blob/master/dist/vue-simple-lightbox.js"></script>

Upvotes: 0

Related Questions