Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass a component as props and use it in a child Component in Vue?

In a Vue 2.0 app, let's say we have components A, B and C.

A declares, registers and uses B

Is it possible to pass C from A to B?

Something like this:

<template>   <div class="A">     <B :child_component="C" />   </div> </template> 

And use C in B somehow.

<template>   <div class="B">     <C>Something else</C>   </div> </template> 

The motivation: I want to create a generic component B that is used in A but receives from A its child C. Actually A will use B several times passing different 'C's to it.

If this approach is not correct, what is the proper way of doing it in Vue?

Answering @Saurabh

Instead of passing as props, I tried the suggestion inside B.

<!-- this is where I Call the dynamic component in B -->  <component :is="child_component"></component>  //this is what I did in B js components: {  equip: Equipment },  data () {  return {    child_component: 'equip',    _list: []  } } 

Basically I'm trying to render Equipment, but the dynamic way

I get 3 errors in console and a blank page

[Vue warn]: Error when rendering component at /home/victor/projetos/tokaai/public/src/components/EquipmentFormItem.vue:

Uncaught TypeError: Cannot read property 'name' of undefined

TypeError: Cannot read property 'setAttribute' of undefined

Apparently I'm doing something wrong

like image 756
Victor Ferreira Avatar asked Mar 24 '17 06:03

Victor Ferreira


People also ask

Can we pass component as a prop in Vue?

To pass a component as props and use it in a child component in Vue. js, we can use the component component. to add the component component. We set the is prop to the childComponent string which has the name of the component we want to render.

How do you pass props to a child component?

There is no way to pass props up to a parent component from a child component. We will revisit this caveat later in this tutorial. It's also important to note that React's props are read only (immutable). As a developer, you should never mutate props but only read them in your components.

How do you access props in data Vue?

To access props in a Vue. js component data function, we can get them from this . to register the messageId prop. Then we get the initial value of the messageId prop with this.


2 Answers

Summing up:

<!-- Component A --> <template>   <div class="A">     <B>       <component :is="child_component"></component>     </B>   </div> </template>  <script> import B from './B.vue'; import Equipment from './Equipment.vue';  export default {   name: 'A',   components: { B, Equipment },   data() {     return { child_component: 'equipment' };   } }; </script>  <!-- Component B --> <template>   <div class="B">     <h1>Some content</h1>     <slot></slot> <!-- Component C will appear here -->   </div> </template> 
like image 86
Jonatas Walker Avatar answered Sep 20 '22 23:09

Jonatas Walker


You can use special attribute is for doing this kind of thing. Example of dynamic component and it's usage can be found here.

You can use the same mount point and dynamically switch between multiple components using the reserved element and dynamically bind to its is attribute:

Your code will look like following:

<template>   <div class="B">     <component :is="child_component"> Something else</component>   </div> </template> 
like image 42
Saurabh Avatar answered Sep 20 '22 23:09

Saurabh