Can you post your AlertSystem.vue?
Spark + Vueify + Custom Vue components
Hey,
I am trying to integrate an existing application to Laravel Spark and I'm having troubles with my Vue components. Vue.js and ES6 is still quite new to me, so I hope you can help me to set this up with Spark.
Let's start with the setup of my existing project where everything is working:
// gulpfile.js
var elixir = require('laravel-elixir');
require('laravel-elixir-vueify');
elixir(function (mix) {
mix.browserify('main.js')
mix.scripts([
...
])
.sass("styles.scss")
.version("css/styles.css")
});
// main.js
import Vue from 'Vue';
import AlertSystem from './components/AlertSystem.vue';
new Vue({
el: 'body',
components: {AlertSystem},
});
Now in my new Spark application I tried to integrate my Alert component like that.
// Spark app gulpfile.js
var elixir = require('laravel-elixir');
require('laravel-elixir-vueify');
elixir(function (mix) {
mix.less('app.less')
.sass("styles.scss")
.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');
});
// app.js file require('spark-bootstrap');
require('./components/bootstrap');
import Vue from 'Vue';
import AlertSystem from './components/AlertSystem.vue';
var app = new Vue({
mixins: [require('spark')],
components: {AlertSystem},
});
So when I log into the application and I get to the "home" view I get this error:
app.js:39283 Uncaught TypeError: Cannot read property 'get' of undefined
which points to this:
this.$http.get('/notifications/recent').then(function (response) {
_this5.notifications = response.data;
_this5.loadingNotifications = false;
});
So can anybody tell me what I am doing wrong and how I can use my given Vue components with vueify in Spark?
Okay I figured it out @christophrumpel.
So if you are getting the following error:
[Vue warn]: Unknown custom element: <test-component> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
It is because you are attaching your AlertSystem-component to the main Vue instance, which is the same thing as you wanting to register the component globally. So, now that we have the global and component keywords, the Vue.js doc says the following:
A component like the above will result in a “max stack size exceeded” error, so make sure recursive invocation is conditional. When you register a component globally using Vue.component(), the global ID is automatically set as the component’s name option. source: http://vuejs.org/guide/components.html#Recursive-Component
So what we learned now is that if we want a global component we should do the following:
import AlertSystem from './components/alert-system.vue';
Vue.component('alert-system', AlertSystem);
var app = new Vue({
mixins: [require('spark')]
});
Now the actual simple reason you are getting your error, is that you are using your <alert-system></alert-system> inside your <home></home>-component.
The Vue app is registered by default on #spark-app-element in resources/views/vendor/spark/layouts/app.blade.php, and spark then @yield('content') and in the home.blade.php-file it, as we expected, uses the <home></home>-component in the "content" section.
So as a round-up, if you want your component to be available everywhere you should do:
import BestComponentEver from './components/best-component-ever.vue';
Vue.component('best-component', BestComponentEver);
But if you just want to add your AwesomeEditor-component to, let's say, the pre-existing home-component you can make add the component as you were trying:
// the home-component is located in ./components by default
// therefore we just import from ./
import AwesomeEditor from './awesome-editor.vue';
Vue.component('home', {
components: {
AwesomeEditor
}
props: ['user'],
ready() {
//
}
});
Now the component is registered we can in resources/views/home.blade.php add our new AwesomeEditor-component:
@extends('spark::layouts.app')
@section('content')
<home :user="user" inline-template>
<div class="container">
<!-- Application Dashboard -->
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<awesome-editor></awesome-editor>
<div class="panel-body">
Your application's dashboard.
</div>
</div>
</div>
</div>
</div>
</home>
@endsection
I do hope that this makes sense.
Please or to participate in this conversation.