F
F
furrya_black2017-06-05 14:00:03
JavaScript
furrya_black, 2017-06-05 14:00:03

How to inject a component into a vue js component?

Vue.component('ComponentName', ComponentDefinition);
...
props: { 
    compName: { type: String, default: '' } 
    propsData: { type: Object, default: {} }
}
<component :is="compName" v-bind=propsData>
<!-- вот тут compName  строка с именем ранее зар-ного компонента -->

It works, but what if I want to pass not the component name, but the component object itself?
import comp from '@/path/comp.vue';
...
props: { 
    comp: { type: Object, default: {} } 
    propsData: { type: Object, default: {} }
}
<component :is="comp" v-bind=propsData>
<!-- вот тут comp уже передан как объект, не строка с именем ранее зар-ного компонента -->

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2017-06-05
@furrya_black

If you use a modular assembly system, then you can do this:

<template>
  <component :is="comp" v-bind=propsData>
</template>

<script>
import comp from '@/path/comp.vue';

export default {
  props: ['propsData'],
  data() {
    return {
      comp
    }
  }
}
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question