3

I have a string (example, because it's an object with many key/values, want to loop and append to htmloutput) with a component name. Is it possible to render/build the component inside a method and display the html output?

Is that possible and how can i achieve that?

     <template>
         <div v-html="htmloutput"></div>
     </template>


<script>
    export default {
        component: {
            ComponentTest
        },
    data() {
            return {
                htmloutput: ''
            }
        },
    methods:{
        makeHtml(){
            let string = 'component-test';//ComponentTest
            //render the ComponentTest directly
            this.htmloutput = ===>'HERE TO RENDER/BUILD THE COMPONENTTEST'<==
        }
    },
    created(){
            this.makeHtml();
    }
</script>

4 Answers 4

2

You might be looking for dynamic components:

https://v2.vuejs.org/v2/guide/components-dynamic-async.html

Example:

<template>
    <component :is="changeableComponent">
    </component>
</template>

<script>
    import FirstComponent from '@/views/first';
    import SecondComponent from '@/views/second';

    export default {
        components: {
            FirstComponent, SecondComponent
        },
        computed: {
            changeableComponent() {
                // Return 'first-component' or 'second-component' here which corresponds
                // to one of the 2 included components.
                return 'first-component';
            }
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, i know this but i want to render it into a method and append the component html to htmloutput
i dont quite see what you want to do. Appending content can be done using different tags in your template right?
I want to call makeHtml and render, created() calls makeHtml()
render() is an alternative to string templates. Loading another component will create a template either through render() or the vue compiler will find your <template> element in that same file.
How can i achieve that?
|
0

Maybe this will help - https://forum.vuejs.org/t/how-can-i-get-rendered-html-code-of-vue-component/19421

StarRating is a sample Vue component. You can get it HTML code by run:

new Vue({
  ...StarRating,
  parent: this,
  propsData: { /* pass props here*/ }
}).$mount().$el.outerHTML

in Your method. Remember about import Vue from 'vue'; and of course import component.

Comments

0

What you're trying to do really isn't best practice for Vue.

It's better to use v-if and v-for to conditionally render your component in the <template> section.

Comments

0

Yes you can use the render function for that here is an example :

Vue.component('CompnentTest', {
  data() {
    return {
      text: 'some text inside the header'
    }
  },
  render(createElement) {
    return createElement('h1', this.text)
  }
})

new Vue({
  el: '#app',

})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <Compnent-test />
</div>

Or :

if you are using Vue-cli :

on your componentTest component :

export default {
  render(createElement) {
    return createElement('h1', 'sometext')
  }
  // Same as
  // <template>
  //   <h1>sometext</h1>
  // </template>
  //
}

and on your root element (App.vue as default) :

export default {
   ....
  component: {
    ComponentTest
  }
}
<template>
  <div>
      ....
     <Component-test />
  </div>
</template>

example : codesandbox

you can read more about Render Functions & JSX

11 Comments

How can i achieve this in my example?
@Bas on your ComponentTest you can create the render function and then just import the component and use it on your template
@Bas i will update the answer for a vue-cli project
Sorry don't understand it, can you use my example?
@Bas yes here is an example : codesandbox check the component
|

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.