首頁 > web前端 > js教程 > 主體

基於vue2.0動態組件及render詳解

亚连
發布: 2018-05-29 16:28:20
原創
1484 人瀏覽過

下面我就為大家分享一篇基於vue2.0動態組件及render詳解,具有很好的參考價值,希望對大家有幫助。

如下:

<template>
 <p 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>
 </p>
</template>
<script>
 //import $ from &#39;@/assets/scripts/lib/zepto.min&#39;
 //console.log($);
 //import Vue from &#39;vue&#39;
 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: &#39;<h3 v-for="item in items">{{ item.c}}</h3>&#39;,
 props: [&#39;myData&#39;],
 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(&#39;p&#39;, Array.apply(null, { length: num }).map(function (v,index) {
  return createElement(&#39;h3&#39;, items[&#39;c_&#39;+index][&#39;c&#39;].toString())
  }))
 } else {
  return createElement(&#39;h1&#39;, &#39;没有数据!&#39;);
 }
 }
 };
 // 注册
 //Vue.component(&#39;my-component&#39;, MyComponent);
export default {
 name: &#39;bar&#39;,
 data () {
 return {
  msg: &#39;Bar&#39;,
  fuck: &#39;Hello&#39;,
  items: data,
  currentView: MyComponent
 }
 },
 methods:{
 addData : function(){
  for(let p in data) {
   if(p == &#39;c_&#39;+num) {
    data[p] = {&#39;c&#39;: num };
   } else {
    data[&#39;c&#39;+num] = {&#39;c&#39;: 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>
登入後複製

上面是我整理給大家的,希望今後會對大家有幫助。

相關文章:

詳解VUE 對element-ui中的ElTableColumn擴充

微信小程式之分享頁面如何傳回首頁的範例

js中el表達式的使用與非空判斷方法

#

以上是基於vue2.0動態組件及render詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!