发布于 2017-05-12 13:18:46 | 188 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Vue.js 教程,程序狗速度看过来!

Vue.js 轻量级 JavaScript 框架

Vue.js 是构建 Web 界面的 JavaScript 库,提供数据驱动的组件,还有简单灵活的 API,使得 MVVM 更简单。


本篇文章主要介绍了详解vue父子模版嵌套案例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

这里是父子模版的调用

这里是针对于vue1.0,如果要学2.0,建议大家去看官方文档

vue2.0 :http://vuefe.cn/guide/

vue-router2.0: https://router.vuejs.org/zh-cn/essentials/getting-started.html

第一种,子组件模版直接写在js里


//定义模版挂载点my-component
<div id="exampleBox1"> 
  <com-ponent></com-ponent>
</div>
<script src="../vue/node_modules/vue/dist/vue.js"></script>
<script> 
  var Component = Vue.extend({// 定义
  template: '<div>A custom component!</div>',
  data: function () {
    return {
      name: 'yuxie' 
    }
  }
});
Vue.component('com-ponent', Component);// 注册 
//注意,extend(json) 和 vue.component('com-ponent', json)//这两个JSON是相等的。
//所以下面第二种会将extend()函数省略掉,直接在component中定义,系统会自动调用extend函数。
  var conp = new Vue({// 创建根实例
    el: '#exampleBox1' 
   });
</script>

第二种,使用HTML模版


<!-- 父组件模板 -->
<div id="exampleBox2" style="border:1px solid #ccc;width:500px;">
  <div>{{parent.name}}</div>
  <!--模版挂载标识-->
  <children></children>
</div>
<!-- 子组件模板 -->
<template id="child-template"> 
  <p style="background:#eee;">{{text}}</p>
</template>
<script> 
Vue.component('children', {//child是模版挂载的标签名    
   template: '#child-template',//id对应子组件的ID    
   data: function () {      
     return {        
       text: '这里是子组件的内容'      
     }    
   }  
});  
var parent = new Vue({// 初始化父组件    
    el: '#exampleBox2',    
    data: {      
      parent: {        
         name:'这里是父组件的内容'      
      }      
     }  
 })
</script>

第三种、来一个复杂的


<div id="example">
  <!--  所有的模板挂件,都必须在根实例ID内部,否则找不到挂件  -->
  <my-component></my-component>
  <!-- 模版可以重用多次 ···· 只不过一样的东西没有这个必要 -->
  <child></child>复用一次
  <child></child>复用二次
  <child></child> ···
  <child></child> ···
</div>
<!--比如放在这里是找不到的-->
<child></child>
<script src="../vue/node_modules/vue/dist/vue.js"></script>
<script>
//定义子组件,子组件必须在父组件之前定义。  
var Child = Vue.extend({template: '<div>A child component!</div>'}); 
//定义父组件
var Parent = Vue.extend({
  template: '<div style="border: 1px solid #ccc;width:200px;">Parent<child-component></child-component>父模版内部</div>', 
     components: {
       // 调用子组件 
       'child-component': Child 
     } 
  }); 
  // 注册父组件 
  Vue.component('my-component', Parent);
  //复用子组件。
  Vue.component('child', Child); 
  // 创建根实例,所有组件都需要在根实例之前创建。
  new Vue({ 
    el: '#example' 
  })
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持phperz。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务