1

I'm new to Vue JS and I've read several different articles online and I can't figure out how to display my component. I'm using Laravel 5.5.

I have a login view page with the following:

<div id="app">
    <div class="login-box">
        <login-form></login-form>
    </div><!-- /.login-box -->
</div>

I created a Login.vue page at /components/Auth/Login.vue:

<template>
    <div class="login-box-body">
        <p class="login-box-msg"> Sign in to start your session. </p>
        <form action="{{ url('/login') }}" method="post">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class="form-group has-feedback">
                <input type="text" class="form-control" placeholder="Username" name="username"/>
                <span class="glyphicon glyphicon-user form-control-feedback"></span>
            </div>
            <div class="form-group has-feedback">
                <input type="password" class="form-control" placeholder="Password" name="password"/>
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
            </div>
            <div class="row">
                <div class="col-xs-8">
                    <div class="checkbox icheck">
                        <label>
                            <input type="checkbox" name="remember"> Remember Me
                        </label>
                    </div>
                </div>
                <div class="col-xs-4">
                    <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
                </div>
            </div>
        </form>
        <a href="{{ url('/password/reset') }}"> Forgot Password? </a><br>
    </div>
</template>

<script>
    export default {
        mounted() {
            console.log('Component mounted.')
        }
    }
</script>

I've registered it in the app.js file:

require('./bootstrap');

Vue.component('example', require('./components/Example.vue'));
Vue.component('login-form', require('./components/Auth/Login.vue'));

const app = new Vue({
    el: '#app',

});

I'm getting the error, [Vue warn]: Unknown custom element: <login-form> - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in root instance).

I've registered it in app.js as seen above and if I use <example></example> it appears correctly.

I changed the Example.vue to the template file and it didn't update -it's still showing the old template. How can I get this to appear?

5
  • 1
    try to provide a name for your login component as it says in the error text. to do this add name: 'login-form', in the exported object at Login.vue Commented Feb 10, 2018 at 22:24
  • @oniondomes like this? export default { name:'login-form', mounted() { console.log('Component mounted.') } } - tried that and still getting the same error. Commented Feb 10, 2018 at 22:32
  • I just trying to help by gues, but try to get rid of these inline requires. const Login = require('./components/Auth/Login.vue'); right after the bootstrap line. If it still doesn't work, examine its value Commented Feb 10, 2018 at 22:55
  • Can you update your question and include code from bootstrap.js file in resources/assets/js folder? Commented Feb 10, 2018 at 23:53
  • @NikolaGavric its the default laravel setup...github.com/laravel/laravel Commented Feb 11, 2018 at 4:32

2 Answers 2

3

I recall struggling with the same issue a few weeks back.

In short form, the fix is to not do it like that.

Instead, you want to register each component explicitly as such:

require('./bootstrap');

const Example =  require('./components/Example.vue');
const Login = require('./components/Auth/Login.vue');

const app = new Vue({
    el: '#app',
    components: { Example, Login }
});

The above has to be done not only for the Vue but for all nested components as well.

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

2 Comments

While this might fix the issue, here is the reference registering global and local components: vuejs.org/v2/guide/components.html#Using-Components
Hmm, still having some issues. I'm trying to use the tag, <login-form> and with Laravel it seems like it's not even picking up this vue file in resources/js/app.js do I need to move this to public/js/..etc? This is the base install of Laravel - nothing changed.
0

Your problem is what I thought it is, but I thought that line was in bootstrap.js thats why I wanted to see that file, anyhow, you will notice how you never imported the Vue inside of your app.js and from the laravel starter project, that line is inside app.js not bootstrap.js and just by looking at your app.js I saw you are missing

window.Vue = require('vue');

So my guess is by adding this line just under require('./bootstrap.js'); your app will register bothVue` components

Comments

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.