基于vue2.0动态组件及render详解
栏目: 编程语言 · JavaScript · 前端 · 发布时间: 6年前
内容简介:下面小编就为大家分享一篇基于vue2.0动态组件及render详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
如下所示:
<template> <div class="hello"> <h1>{{ msg }}</h1> <h2>这里是Boor</h2> <component v-bind:my-data="items" v-bind:is="currentView"> <!-- 组件在 vm.currentview 变化时改变! --> </component> <a class="explain">直直</a> <button v-on:click="addData">点击</button> </div> </template> <script> //import $ from '@/assets/scripts/lib/zepto.min' //console.log($); //import Vue from 'vue' function isEmptyObject(e) { var t; for (t in e) return !1; return !0 } function objectLength(o) { var len = 0; for(var p in o) { len++; } return len; } let data ={ c_0:{c:1}, c_1:{c:2}, c_2:{c:3}, c_3:{c:4} }; let num = 0; //console.log(objectLength(data)); const MyComponent = { //template: '<h3 v-for="item in items">{{ item.c}}</h3>', props: ['myData'], data(){ return{ //items : myData } }, render: function (createElement) { debugger; let items = this.myData; //items = JSON.stringify(items); let num = objectLength(items); if (!isEmptyObject(items)) { debugger; return createElement('div', Array.apply(null, { length: num }).map(function (v,index) { return createElement('h3', items['c_'+index]['c'].toString()) })) } else { return createElement('h1', '没有数据!'); } } }; // 注册 //Vue.component('my-component', MyComponent); export default { name: 'bar', data () { return { msg: 'Bar', fuck: 'Hello', items: data, currentView: MyComponent } }, methods:{ addData : function(){ for(let p in data) { if(p == 'c_'+num) { data[p] = {'c': num }; } else { data['c'+num] = {'c': num }; } } num++; } } }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> h1, h2 { font-weight: normal; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style>
以上所述就是小编给大家介绍的《基于vue2.0动态组件及render详解》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
人人都是产品经理
苏杰 / 电子工业出版社 / 2010年4月 / 45.00元
这是写给“-1到3岁的产品经理”的书,适合刚入门的产品经理、产品规划师、需求分析师,以及对做产品感兴趣的学生,用户体验、市场运营、技术部门的朋友们,特别是互联网、软件行业。作为一名“4岁的产品经理”,作者讲述了过去3年的经历与体会,与前辈们的书不同,本书就像你走到作者身边,说“嗨哥们!晚上有空吃个饭么,随便聊聊做产品的事吧”,然后作者说“好啊”。 书名叫“人人都是产品经理”,是因为作者觉得过......一起来看看 《人人都是产品经理》 这本书的介绍吧!