0

I just want to ask how to bind whole component (for example div) in Vue.js. Is there anything like innerHTML? Here is example: Parent.vue

<template>
  <div id="parent">
   //some elements
  </div>
</template>

Child.vue

<template>
  <div id="child">
   //some different elements
  </div>
</template>

Now how to innerHTML child in parent? I've tried something like v-html:component and then data(){ return{ component: and here I dont know how to pass whole vue Component like Child.vue div. Should I use refs or something?

Now I use visibility attribute from css and I change it but I don't think that is good way to do this.

5
  • 1
    Possible duplicate of Passing html into Vue component Commented Jun 15, 2019 at 20:21
  • What is the actual problem you're trying to solve? Can you give us a more direct example. In 99% cases props are the way to go to share data from parent to child and events to share data from child to parent. You rarely need to pass the whole component. Commented Jun 15, 2019 at 20:27
  • I also think slots is the best answer like @str if you wanna split html check [JSX and reder of Vue][vuejs.org/v2/guide/render-function.html] [Check Slots from Vue here][vuejs.org/v2/guide/components-slots.html] Commented Jun 15, 2019 at 20:48
  • 1
    @Bergur I have main container and I have menu table and table for product in it, I want to switch between them in this main container. Now I do something like changeing visibility, when one is visible another is not. I dont know if this is a good way so I want to find a way to innerHTML this mainContainer div. Commented Jun 15, 2019 at 21:14
  • Depending on what problem you're trying to solve, slots might be what you're looking for. But, as already pointed out, without insight on the actual problem, we can only guess. Please provide a minimal reproducible example and specify in clear what you're trying to achieve. Commented Jun 15, 2019 at 22:01

1 Answer 1

1

If you want to switch between components, then check out VueJS dynamic components: https://v2.vuejs.org/v2/guide/components.html#Dynamic-Components

You can use the component element and the :is prop to send down what component to render.

I have a working demo here: https://codepen.io/bergur/pen/bPEJdB

Imagine the following simple Vue component

Vue.component('menuList', {
  data() {
    return {
      list: ['Menu item A', 'Menu item B']
    }
  },
  template: `
   <ul>
    <li v-for="item in list">{{ item}}</li>
   </ul>
  `
})

This is a simple component rendering a unordered list of menu items. Lets create another similiar component that renders ordered list of products. Note that just to make them a little different, the menuList that has ul and the productList has ol

Vue.component('productList', {
  data() {
    return {
      list: ['Product item A', 'Product item B']
    }
  },
  template: `
   <ol>
    <li v-for="item in list">{{ item}}</li>
   </ol>
  `
})

Now we can create a main VueJS that renders these components depending on which button I press. You can have what ever trigger/action you want to change the component.

new Vue({
  name: 'main',
  el: '#main',  
  data() {
    return {
      header: 'Component switching',
      selectedComponent: 'menuList'
    }
  },
  methods: {
    setComponent(name) {
      this.selectedComponent = name
    }
  },
  template: `<div>
    <button @click="setComponent('menuList')">Menu List</button>
    <button @click="setComponent('productList')">Products</button>
    <component :is="selectedComponent" />
  </div>`
})

So here the magic begins.

We create a app with some data properties. The header property is just a string value, and selectedComponent tells us which component is beeing rendered.

In our template we use the <component :is="selectedComponent /> so initially the menuList component is the active one.

We create a method called setComponent that takes in a string value and sets that as a new value for selectedComponent. By pressing a button a new value for selectedComponent is set and the component is rendered. Voila

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, nice answer :) Many thanks to you and your time <component :is="selectedComponent" /> is what I am looking for. Cheers and thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.