How to use VueJS 2 global components inside single file components?

大城市里の小女人 提交于 2019-12-03 01:22:37

You don't need the module.exports. You can register the component globally by having this within the mycomponent.vue file.

<template>
    <div>A custom component!</div>
</template>
<script>
    export default {}
</script>

Then add to main.js

import MyComponent from './component.vue'
Vue.component('my-component', MyComponent);

or I typically register them in a 'globals' file them import that into the main.

That should then allow you to use my-component anywhere in the app.

Component.vue

<template><div>A custom component!</div></template>

<script>export default { code here... }</script>

Use this component in home.vue:

<template>
    <div>
        <my-component></my-component>
    </div>
</template>

 <script>
    import component from './component.vue'
    export default {
     components: { my-component: component }
    }
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!