Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaner way to require multiple Vue components?

I've just started working with Vue.JS and there's one small issue that's bugging me. My file structure similar to the following:

+ js
|--+ components
|  |-- parent.vue
|  |-- child.vue
|-- main.js

Then in my main.js I have the following:

window.Vue = require('vue');
require('vue-resource');
Vue.component('parent', require('./Components/parent'));
Vue.component('child', require('./Components/child'));
var app = new Vue({ el: "#app" });

(I'm not actually certain what vue-resource is, but this was set up for me by a fresh install of Laravel 5.3)

At a glance I immediately noticed that my main.js file was going to get unmanageable if I added too many components. I don't have this issue when working with ReactJS because main.js only needs to include the "parent" component, and the parent component includes the child component. I figured Vue.JS would have a similar trick to help me organize my components - but reading through the docs I didn't find one (maybe I missed it?)

Is there a way to either have a Vue component list its dependencies (for Browserify / Webpack to bundle) or recursively run a javascript statement on every file in a directory (so Browserify / Webpack just packs up the whole thing)?

I'm not concerned with async components at the moment - so if the solution breaks that functionality it will be okay. One day I would like to play around with using Webpack to create async components and only loading them as I need them, but today I'm more interested in just getting this up and running so I can play way Vuex.

like image 627
stevendesu Avatar asked Dec 16 '16 19:12

stevendesu


People also ask

How do I make reusable components Vue?

Each component you create in your Vue app should be registered so Vue knows where to locate its implementation when it is encountered in a template. To register your component in another component, you should add it to the components property in the Vue object.

Can you have multiple Vue instances?

It is totally legal and fine to have multiple Vue instances on your page controlling different parts and pieces of it. In fact, you can even have your instances communicate with each other by storing them in variables within the global namespace.


2 Answers

The Vue.component syntax is for global components only, if you have a component that is being used inside another component use this:

import Parent from './components/Parent.vue';
import Child from './components/Child.vue';

new Vue({ 
  el: "#app", 
  components: { Parent, Child } 
});

Than inside this components you can use the other components.

The only advantage of using Vue.component(Parent) is that you can use this <parent></parent> component globaly in all your other components without declaring them implicitly.

Good Luck :)

like image 73
AfikDeri Avatar answered Nov 10 '22 04:11

AfikDeri


You don't need to import everything at the top level.

In your main.js you can import the Parent component

import Parent from './components/Parent.vue'

new Vue({
  el: "#app",
  components: {
    Parent
  }
})

With your Parent.vue

<template>
  <div>
    <p>I am the parent</p>
    <child></child>
  </div>
</template>

<script>
  import Child from './Child.vue'

  export default {
    mounted() {
      console.log('mounted parent')
    }
  }
</script>

<style scoped>
  // ...
</style>

Then in your Child.vue

<template>
  <p>I am the child</p>
</template>

<script>
  export default {
    mounted() {
      console.log('mounted child')
    }
  }
</script>

<style scoped>
  // ...
</style>

And you should end up with

<div>
  <p>I am the parent</p>
  <p>I am the child</p>
</div>
like image 5
darryn.ten Avatar answered Nov 10 '22 05:11

darryn.ten