【问题标题】:Rendering Dynamic Components in vue.js?在 vue.js 中渲染动态组件?
【发布时间】:2016-11-14 20:49:41
【问题描述】:

我正在尝试通过使用 set timeout 伪造 ajax 调用来在 vue.js 中呈现动态组件。它应该注册一个新组件,然后在视图中设置它!我正在使用 vue.js 1。请告诉我一个解决方案,让我知道如何让它工作。

<!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>

问题:

我正在使用 Vue.js 1,但无法插入组件的视图!我认为不再使用v-component 了

【问题讨论】:

    标签: javascript vue-component vue.js


    【解决方案1】:

    当您进行两项小改动时,您的代码可以在 Vue.js 1 中正常工作:

    1. 使用&lt;component :is="currentView"&gt;&lt;/component&gt; 包含组件。 v-component 已经在 0.12.0 中删除了很久。

    2. 使用&lt;div v-for="value in items"&gt;{{value}}&lt;/div&gt; 迭代数组。 v-repeat 已在 1.0 中弃用。

    这是一个有效的JSFiddle。

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2017-08-17
      • 2021-12-28
      • 1970-01-01
      • 2016-12-21
      • 2018-01-24
      • 1970-01-01
      • 2019-08-07
      • 2019-08-18
      相关资源
      最近更新 更多