Search code examples
phplaravelvue.jslaravel-spark

Using Vue Custom Components in Laravel Spark


I'm pretty new to using browserify etc. for js development. Also I'm new to Vue. I wanted to create additional custom components for my application. However I fail at so many points even when following different tutorials step by step.

This is What I currently got:

example.js

var MyComponent = Vue.extend({
  data: function() {
    return { message: 'This is a test' }
  },

    template: '{{ message }}'
  });

Vue.component('my-component', MyComponent);

new Vue({
  el: '#example'
});

app.js

require('spark-bootstrap');

require('./../example.js');

require('./components/bootstrap');

require('./backoffice/main.js');

var app = new Vue({
    mixins: [require('spark')]
});

view.blade.php

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

gulpfile.js

elixir(function(mix) {
    mix.less('app.less')
       .browserify('app.js', null, null, { paths: 'vendor/laravel/spark/resources/assets/js' })
       .copy('node_modules/sweetalert/dist/sweetalert.min.js', 'public/js/sweetalert.min.js')
       .copy('node_modules/sweetalert/dist/sweetalert.css', 'public/css/sweetalert.css');
});

My error

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

Solution

  • Since example.js is a separate module, you'll need to import Vue in that file as well. I assume you've done that. You also don't want to new up a Vue instance in that file though, because you're already doing that later on. If <my-component> is inside of the spark body you already have a Vue app running.