0

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 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Do I have to include the <div id="app"> in my blade layout file or do I include it in the specific blade view where I want to include the Vue component?
I always put it on the general layout so I can use Vue in all my views in case I need it. For clarity, I forgot to say that you should compile your js and include it.
I am not getting the example-component to work even though I do this. It isn't rendering in my view. Nothing is happening. There is no error either. It just doesn't seem to work. I want to render the HTML markup in the ExampleComponent.vue
It says that "could not find declaration file for vue-star-rating" in the line: var StarRating = require('vue-star-rating');
0

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>

2 Comments

If I do what you said, can I run the laravel app without php artisan serve command ?
@IstiaqueAhmed You still need to serve your app, so it is yet needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.