Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 VueJS not working

I'm trying to use vuejs in my fresh laravel setup. I run the follwing commands in my command line

npm install
npm run dev

This commands runs without any errors. When I import the default VUE componet located under ~/ressources/assets/components/ExampleComponent.vue in my template nothing happends.

@section('content')
    <example-component></example-component>
@endsection

Here the app.js

require('./bootstrap');

window.Vue = require('vue');

Vue.component('example-component', require('./components/ExampleComponent.vue'));

const app = new Vue({
    el: '#app'
});
like image 579
Markus Avatar asked Mar 03 '18 20:03

Markus


3 Answers

If you are using Laravel Mix check your webpack.mix.js file, default config must look like this

 mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

Now, look if you have linked JS file to you blade template

Looks like this for older Laravel versions (<= 5.5):

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

and like this for new once (> 5.5)

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

Finally, see you have

el: '#app'

it mean # - id, like in CSS, VueJS will search for element with id app. Everything outside that element will not work.

so you have to use it like

<div id='app'> 
<example-component></example-component>
</div>

or

<section id='app'> 
    <example-component></example-component>
    </section>
like image 114
dav88dev Avatar answered Nov 16 '22 10:11

dav88dev


I think you should wrap the container div with an id of app

<div id="app" > <example-component></example-component> </div>

if not just check if the Js files are properly imported in the php file

like image 38
hamza Avatar answered Nov 16 '22 11:11

hamza


I also faced this problem and mine is solved. I'm not importing

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

check that you are importing js/app.js and #app

It should work for you.

like image 1
Tarik Manoar Avatar answered Nov 16 '22 11:11

Tarik Manoar