Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'import' a Vue component into a Vue component

This may be a really silly question, but nonetheless, something I can't find an answer to.

So, I am building up a Vue component and would like to access vue-spinner's component(s) inside of my component. How would I do that?

Here are snippets of the code in question:

app.js:

Vue.component('players', require('./components/TeamPlayersComponent.vue'));

import GridLoader from 'vue-spinner/src/GridLoader.vue';

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

TeamPlayersComponent.vue:

<grid-loader></grid-loader>

Assume that vue-spinner has been installed (NPM) and that the TeamPlayersComponent.vue is valid and working other than giving this error in the console:

vue.js?3de6:525 [Vue warn]: Unknown custom element: <grid-loader> - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in component <players> at /home/vagrant/code/rounds-smaa/resources/assets/js/components/TeamPlayersComponent.vue)

I am using Vue with Laravel Elixir and Gulp.

like image 644
AshMenhennett Avatar asked Jan 04 '17 09:01

AshMenhennett


People also ask

How do I transfer Vue component to another component?

You need to create your portfolio component first e.g. in src/components/Projects/Portfolio. vue and then import it inside the script tag within your Landing.

How do I register a Vue component?

When we want to globally register a component in Vue, we need to do it in this file. All you have to do is import your component, as you usually would, and then register it using app. component . import { createApp } from 'vue' import App from './App.

Can you use Vue 2 components with Vue 3?

There are a number of breaking changes from Vue 2 to 3. You won't be able to just use Vue 2 components in Vue 3 without accounting for these.


2 Answers

You can get it by following:

var PulseLoader = require('vue-spinner/src/PulseLoader.vue');

and add it in components:

new Vue({
  components: {
    'PulseLoader': PulseLoader
  }
})
like image 59
Saurabh Avatar answered Oct 07 '22 00:10

Saurabh


Solution:

Replace the GridLoader specific code in app.js (Laravel setup) with:

Vue.component('grid-loader', require('vue-spinner/src/GridLoader.vue'));

Worked as expected!

like image 26
AshMenhennett Avatar answered Oct 06 '22 23:10

AshMenhennett