0

I'm starting to use vue js in my Laravel project. I installed all packages, but my problem is when I try to call a component inside another component and I run this command :

npm run dev

I had an error from webpack.

My Code:

the parent component TabelReception.vue:

<template>
<stats-card></stats-card>
    <table class="table">
        <tbody>
        </tbody>
    </table>
</template>
<script>
    import StatsCard from './StatsCard'
    Vue.component('StatsCard', StatsCard)
    export default {
        components: {
            'stats-card': StatsCard 
        }
    }
</script>

The soon component StatsCard.vue:

<template>
<h2>
hi bro just a test 
</h2>
</template>
<script>
    export default {
    }
</script>

app.js:

import Vue from '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('reception-table', require('./components/TableReception.vue').default);
Vue.component('stats-card', require('./components/StatsCard.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 core = new Vue({
    el: '#core'
});

please help .

1
  • Hi, can you post the errors you're getting? Describing your configuration may also be helpful to assist you in working out the problem Commented Apr 12, 2020 at 0:58

2 Answers 2

2

The component allow only single element in the template section. If you need to use multiple, wrap it with div and make it single like

<template>
  <div>
    <stats-card></stats-card>  
    <table class="table"> 
        <tbody>

        </tbody>
    </table>
  </div>  
</template>

You don't need to use both methods to register a component

<script>
   import StatsCard from './StatsCard'

   export default {
        components: {
          StatsCard
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

good its working now so is required to put it inside a div for multiple elements , and what about app.js if i remove : Vue.component('stats-card', require('./components/StatsCard.vue').default); and call directly from the component ?
It will work. Vue.component() is to register a component globally.
0

<template>
    <div>
        <stats-card>
        </stats-card>
    
        <table class="table">
    
    
            <tbody>
    
    
            </tbody>
        </table>
    </div>
</template>

<script>
import StatsCard from './statccard'


export default {
    components: {

        'stats-card': StatsCard
    }
}
</script>

the  template root on requires one ony one element but you have also iported the component wrongly

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.