基于vue2.0動(dòng)態(tài)組件及render詳解
更新時(shí)間:2018年03月17日 12:52:16 作者:huang100qi
下面小編就為大家分享一篇基于vue2.0動(dòng)態(tài)組件及render詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
如下所示:
<template>
<div class="hello">
<h1>{{ msg }}</h1>
<h2>這里是Boor</h2>
<component v-bind:my-data="items" v-bind:is="currentView">
<!-- 組件在 vm.currentview 變化時(shí)改變! -->
</component>
<a class="explain">直直</a>
<button v-on:click="addData">點(diǎn)擊</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', '沒有數(shù)據(jù)!');
}
}
};
// 注冊(cè)
//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動(dòng)態(tài)組件及render詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VUE+elementui面包屑實(shí)現(xiàn)動(dòng)態(tài)路由詳解
今天小編就為大家分享一篇VUE+elementui面包屑實(shí)現(xiàn)動(dòng)態(tài)路由詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue-cli整合vuex的時(shí)候,修改actions和mutations,實(shí)現(xiàn)熱部署的方法
今天小編就為大家分享一篇vue-cli整合vuex的時(shí)候,修改actions和mutations,實(shí)現(xiàn)熱部署的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue3使用v-if指令進(jìn)行條件渲染的實(shí)例代碼
條件渲染是根據(jù)條件的真假來有條件地渲染元素,在Vue.js 3.x中,常見的條件渲染包括使用v-if指令和v-show指令,本文講解使用v-if指令進(jìn)行條件渲染,需要的朋友可以參考下2024-03-03
詳解vue computed的緩存實(shí)現(xiàn)原理
這篇文章主要介紹了vue computed的緩存實(shí)現(xiàn)原理,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下2021-04-04

