0

Can I pass in a template string and also dynamically pass in a property so that I can make it reactive? In the example below I would like message to be reactive, but I don't want to have to predefine it on the data option.

<div id="vue">
 <component :is="string && {template:string}"/>
</div>

new Vue({
    el:'#vue',
    data(){
    return {
    string:undefined,
   }
  },
  created(){
    //setTimeout to simulate ajax call
    setTimeout(()=> this.string = '<div><h1 v-for="n in 1">Hello!    </h1><input v-model="message" placeholder="edit me"><p>Message is: {{ message }}</p>    </div>', 1000)
  }
})

https://jsfiddle.net/kxtsvtro/5/

1
  • Are you looking for v-bind? Commented Feb 15, 2018 at 17:53

3 Answers 3

6

You can specify the data in the same way you specify the template: just interpolate it into the component spec.

new Vue({
  el: '#vue',
  data() {
    return {
      string: undefined,
      dataObj: undefined
    }
  },
  created() {
    //setTimeout to simulate ajax call
    setTimeout(() => {
      this.string = '<div><h1 v-for="n in 1">Hello!</h1><input v-model="message" placeholder="edit me"><p>Message is: {{ message }}</p></div>';
      this.dataObj = {
        message: 'initial'
      };
    }, 1000)
  }
})
<script src="//unpkg.com/vue@latest/dist/vue.js"></script>
<div id="vue">
  <component :is="string && {template:string, data: function() { return dataObj}}" />
</div>

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

4 Comments

Can I also pass in a function dynamically for computed? computed: { }
I would think so. Give it a shot. You could have a single data object for the entire component spec and populate whatever pieces of it you need to.
Is there a way to load or populate the entire componentspec (AJAX)? I'm hung up on passing in a computed function. I might be going about this all wrong. jsfiddle.net/ot9dh97u/22
Ajax doesn't support functions. You would have to serialize and unserialize the function
0

It looks like what you want is a component with a <slot> that you can dump a custom component into. If you're trying to compose components that's probably the easiest way to do it.

Comments

0

You can use Props, but I am not sure if this is the best way to do it :) here is an example:

new Vue({
    el:'#vue',
    data(){
    return {
        string:undefined,
      message:''
    }
  },
  created(){
    //setTimeout to simulate ajax call
    setTimeout(()=> this.string = '<div><h1 v-for="n in 1">Hello!</h1><input v-model="message" placeholder="edit me"><p>Message is: {{ message }}</p></div>', 1000)
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script> 
<div id="vue">
  <component :is="string && {template:string, props:['message']}" :message="message"/>
</div>

2 Comments

Is there any way to set a dynamic prop on the component? I don't want it to be always called "message".
See Roy's solution i think it is better

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.