0
votes

I'm new to using Vue.js and I am trying to learn how to implement a rating system for recipes in my Laravel application using vue-star-rating. The first thing I want to learn is how to build components and then include them in my blade views. Already here I am running into trouble. Trying to include the ExampleComponent.vue that comes with Laravel out of the box, but I don't understand how I am to include it in my blade file. The top of my blade file looks like this

Blade view

<div class="col-sm-6">
                <div class="container recipeDesc">
                    <h2 class="text-center name">{{$recipe->title}}</h2>
                        <div class="stars">
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                            <i class="fas fa-star"></i>
                        </div>

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

Default JS file

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

window.Vue = require('vue');

/**
 * The following block of code may be used to automatically register your
 * Vue components. It will recursively scan this directory for the Vue
 * components and automatically register them with their "basename".
 *
 * Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
 */

// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))

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

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

const app = new Vue({
    el: '#app',
    components: {
        'example-component': require('./components/ExampleComponent.vue'),
    }
});

How would I include the ExampleComponent.vue in this div? Having a hard time understanding this, even when reading the Vue.js documentation. Thank you!

2

2 Answers

0
votes

If you still have the default js files of the Laravel installation, in your resources/js/app.js file you should have the following:

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

const app = new Vue({
    el: '#app'
});

The first line tells you that the name you should use for the example component is example-component, so to include it on your blade view, simply write this:

<example-component/>

The second block in the app.js file tells you that Vue will work on an element with id="app", so you should make sure that you have an div with that id on your layout if you want to use Vue components.

0
votes

Before the body ending tag, you should include the vue script source in order to be able to implement it in your views: <script src="{{ asset('js/app.js') }}"></script></body>