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>
Answer from Jonatas Walker on Stack OverflowIs it possible to pass a component as props and use it in a child Component in Vue?
Vue: Components/templates as props
Passing components as props
Newbie Question - Vue Component Passing props to function
Videos
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>
You can use special attribute is for doing this kind of thing. Example of dynamic component and its 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.
Here's how is can be used with either an imported component or one passed as a prop:
<template>
<div class="B">
<component :is="myImportedComponent">Something</component>
--- or ---
<component :is="myPassedComponent">Something else</component>
</div>
</template>
<script>
import myImportedComponent from "@/components/SomeComponent.vue"
export default {
props: {
myPassedComponent: Object
},
components: {
myImportedComponent
},
}
</script>
If you are trying to inject html in props, you should really be looking at using slots.
https://v2.vuejs.org/v2/guide/components-slots.html
The whole point of using components is that you create a separation of concerns. Defining all the layered components in a single line completely defeats that purpose. Create three separate components. One container component like this.
index.vue
<template>
<tab :name="mycoolname"></tab>
</template>
<script>
import tab from tab.vue
export default {
components: {
tab
}
</script>
The name prop you can than use in that template using {{name}}. Than within the tab component, like this:
tab.vue
<template>
<h1>{{name}}</h1>
<my-custom-component @click="doStuff()"></my-custom-component>
</template>
<script>
import my-custom-component from my-custom-component.vue
export default {
props: [
'name'
],
components: {
my-custom-component
},
methods: {
doStuff () {
console.log('hooray this totally works')
}
</script>
I am using separate build files here, but the concept remains the same when you just import vue into your project. Components can contain everything, from a simple div with a <p> inside to a full page. This component system is insanely powerful to keep things organised.
Without webpack it would look something like this:
var myCustomComponent = {
template: `fancyHTML`
}
var Tab= {
components: {
'my-custom-component': myCustomComponent
},
template: `even more fancy html containing <my-custom-component>`
new Vue({
el: '#app'
components: {
'tab': Tab
}
})
}
So I am now working on two projects. One with Vue and one with React.
One thing I really like doing in React is passing components as props, and I find myself wanting to do it in Vue.
Is there an approach for this in Vue?
Thanks!