2

I am creating a simple Vue.js application with Laravel. I have registered Vue.js with:

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

import App from './components/App';

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

If I create a custom component, this works just fine:

Vue.component('nav-section', {
    template: `<div>This is odd</div>`
});

I can then call <nav-section></nav-section> in any given template, and it will output "This is odd."

However if I use Laravel's require method like so:

Vue.component('nav-section', require('./components/Navigation'));

It is not working anymore. <nav-section></nav-section> is empty, blank. No errors in npm or console. Am I missing something, or some logic behind require?

Navigation.vue:

<template>
    <div>
        <span>Sample text</span>
    </div>
</template>
<script>
export default {}
</script>

Vue.js:

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

import App from './components/App';

Vue.component('nav-section', require('./components/Navigation'));


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

1 Answer 1

3

Try writing it out like this, with the extension and default:

Vue.component('nav-section', require('./components/Navigation.vue').default);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh man, I did not even think to try it like this. Thank you indeed for this solution :-). So as long as default is attached .vue is "still" not necessary :-)
I expected it might, but I figured it wouldn't hurt to be thorough. :)

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.