0

error: Cannot read properties of undefined (reading 'component')

i am working vue.js3 + laravel8

I'm trying to register my reused my-button component as a global component

vue.js3 + lorevel8

my app.js

window.Vue = require('vue').default;
import { createApp } from 'vue';
import router from "./router";
import components from './components/UI'

components.forEach(component => {
    app.component(component.name, component)
})

const app = createApp({})
app.use(router)
   .mount("#app")

my Index.js

import MyButton from "./MyButton";


export default  [
    MyButton
]

my button.vue

<template>
    <button class="btn">
        <slot></slot>
    </button>
</template>


<script>
export default {
    name: "my-button"
}
</script>

<style scoped lang="scss">

.btn {
    background-color: var(--color-primary-light);
    color: #fff;
    border: none;
    border-radius: 0;
    font-family:'Josefin Sans', sans-serif;
    font-size: 1.5rem;
    text-transform: uppercase;
    padding: 1.8rem 3rem;
    cursor: pointer;
    transition: all .2s;

&:hover {
     background-color: var( --color-primary-dark);
 }
}

</style>

error: Cannot read properties of undefined (reading 'component')

Can anybody help me plsss ?

can't figure it out properties of undefined

2 Answers2

0

I also faced this issue. However I managed to resolve this, thus I'm sharing my answer.

Using Vue3 than make the necessary changes in your app.js:

import {
  createApp
} from 'vue';
const app = createApp({});

// You can register your components globally like this also
// Add as many components as you want.

app.component('Name of your component', require('Path to the component').default)

// In your case 
app.component('my-button',require('Path to the component').default);

app.mount('#app');

I think it should solve the issue. Please check.

ouflak
  • 2,458
  • 10
  • 44
  • 49
-1

i stumbled

const app = createApp ({}) 

should come before

components.forEach (component => {
    app.component (component.name, component)
})