Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to render a dynamic component in vue.js by faking an ajax call by using set timeout. It should register a new component then set it in the view! I'm using vue.js 1. Please show me a solution as to how I can get this working please.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>test dynamic components</title>
</head>
<body>
	
	<div v-component="{{currentView}}"></div>


	<script src="vue-v1.js"></script>
	<script>
    //Create the 'default' component
    Vue.component("default", {
        template: '<div>This should be replaced (and will be in 2 seconds)</div>'
    });

    //Create the parent ViewModel
    var vm = new Vue({
        el: "body",
        data: {
            currentView: "default"
        }
    });

    //Pretend to load the data from the server
    //This would actually be $.get("/url", data, function(){...});
    window.setTimeout(function () {
        
        //Create the new component using the template we received
        Vue.component("BoardFeed", {
            template: '<div>Template returned from server, what I really want</div><br>And directives work too:<div v-repeat="items">{{$value}}</div>',
            data: function () {
                return {
                    items: [1, 2, 3]
                };
            }
        });
        
        //And then change the page to that component
        vm.currentView = "BoardFeed";
    }, 2000);
	</script>
</body>
</html>

ISSUE:

I'm using Vue.js 1 and I can't interpolate the view of my component! I don't think using the v-component even is used anymore

share|improve this question
up vote 1 down vote accepted

Your code works fine with Vue.js 1 when you make two small changes:

  1. Use <component :is="currentView"></component> to include the components. v-component has been removed quite some time ago in 0.12.0.

  2. Use <div v-for="value in items">{{value}}</div> to iterate over an array. v-repeat has been deprecated in 1.0.

Here's a working JSFiddle.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.