Vue.js组件—父组件与子组件之间的数据联系
2017-07-27 17:14
716 查看
Vue.js中的组件可以理解为自己定义的标签;而组件化的目的则可以理解为方便代码复用。
父组件和子组件之间是如何进行数据联系的呢?
父组件向子组件传递数据:
html代码如下:
开始,我引入style的方式为:
vue提示的报错信息为:
Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example,instead of <div style="{{ val }}">,use <div :style="val">
改为以下写法:
正确写法为:
html代码如下:
1.定义组件及其中的template模板;
2.当点击button时,会触发组件中的secplus函数;
3.通过emit方法触发父组件中的plustotal监听事件;
4.plustotal触发全局的totalplus函数。
如何在一个页面中同时定义多个组件呢?
使用Vu.extend方法,代码如下:
1.创建组件:使用Vue.extend创建两个组件,并分别保存在counter和seccounter两个对象中;
2.注册组件;
3.使用组件。
也可以使用Vue.component直接注册:
父组件和子组件之间是如何进行数据联系的呢?
父组件向子组件传递数据:
html代码如下:
<div id="app"> <counter heading="完美极了" bgcolor="green" fontsize="16"></counter> //这里的heading,bgcolor,fontsize是要传给子组件的数据 <counter heading="糟糕透了" bgcolor="red" fontsize="16"></counter> </div> <template id="mycounter"> <div class=""> <h1>{{ heading }}</h1> <button type="button" name="button" @click="plus" v-bind:style="{background: bgcolor,fontSize: fontsize + 'px',color: '#fff',border: 'none'}">赞赞赞{{ count }}</button> </div> </template>js代码如下:
<script src="vue.js" type="text/javascript"></script> <script type="text/javascript"> Vue.component("counter",{ template:"#mycounter", data:function(){ return { count : 0}; //组件中的data必须是一个函数 }, props:["heading","bgcolor","fontsize"], //使用props声明从父组件传过来的数据后,子组件才可以使用 methods : { plus : function(){ this.count += 1; } } }); new Vue({ el : "#app" }); </script>注:开发过程中遇到了一个vue.js中style绑定的问题。
开始,我引入style的方式为:
<button type="button" name="button" @click="plus" style="background: {{bgcolor}}">赞赞赞{{ count }}</button>
vue提示的报错信息为:
Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. For example,instead of <div style="{{ val }}">,use <div :style="val">
改为以下写法:
<button type="button" name="button" @click="plus" v-bind:style="background: {{bgcolor}}">赞赞赞{{ count }}</button>依然报错。
正确写法为:
<button type="button" name="button" @click="plus" v-bind:style="{background: bgcolor,fontSize: fontsize + 'px',color: '#fff',border: 'none'}">赞赞赞{{ count }}</button>子组件又是如何触发父组件的呢?
html代码如下:
<div id="app"> <p>{{ total }}</p> <seccounter v-on:plustotal="totalplus"></seccounter> <seccounter v-on:plustotal="totalplus"></seccounter> </div>js代码如下:
<script src="vue.js" type="text/javascript"></script> <script> Vue.component("seccounter",{ template : '<button v-on:click="secplus">{{ seccounternum }}</button>', data : function(){ return { seccounternum : 0 }; }, methods : { secplus : function(){ this.seccounternum += 1; this.$emit('plustotal'); //使用emit触发事件 }, } }); new Vue({ el : "#app", data :{ total : 0 }, methods : { totalplus : function(){ this.total += 1; } } }); </script>大致流程如下:
1.定义组件及其中的template模板;
2.当点击button时,会触发组件中的secplus函数;
3.通过emit方法触发父组件中的plustotal监听事件;
4.plustotal触发全局的totalplus函数。
如何在一个页面中同时定义多个组件呢?
使用Vu.extend方法,代码如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue.js组件</title> </head> <body> <p>父元素给子元素传值</p> <div id="app"> <counter heading="完美极了" bgcolor="green" fontsize="16"></counter> <counter heading="糟糕透了" bgcolor="red" fontsize="16"></counter> <hr /> <p>子元素向父元素通信</p> <p>{{ total }}</p> <seccounter v-on:plustotal="totalplus"></seccounter> //3.使用组件 <seccounter v-on:plustotal="totalplus"></seccounter> </div> <template id="mycounter"> <div class=""> <h1>{{ heading }}</h1> <button type="button" name="button" @click="plus" v-bind:style="{background: bgcolor,fontSize: fontsize + 'px',color: '#fff',border: 'none'}">赞赞赞{{ count }}</button> </div> </template> <script src="vue.js" type="text/javascript"></script> <script type="text/javascript"> var counter = Vue.extend({ //1.创建组件 template:"#mycounter", data:function(){ return { count : 0}; }, props:["heading","bgcolor","fontsize"], methods : { plus : function(){ this.count += 1; } } }); var seccounter = Vue.extend({ //1.创建组件 template : '<button v-on:click="secplus">{{ seccounternum }}</button>', data : function(){ return { seccounternum : 0 }; }, methods : { secplus : function(){ this.seccounternum += 1; this.$emit('plustotal'); }, } }); new Vue({ el : "#app", components : { //2.注册组件 'counter' : counter, 'seccounter' : seccounter }, data :{ total : 0 }, methods : { totalplus : function(){ this.total += 1; } } }); </script> </body> </html>说明:
1.创建组件:使用Vue.extend创建两个组件,并分别保存在counter和seccounter两个对象中;
2.注册组件;
3.使用组件。
也可以使用Vue.component直接注册:
Vue.component({'counter' : counter,'seccounter': seccounter});
相关文章推荐
- Vue.js的组件(三)父组件与子组件的数据联系 之 子传父 之 自定义事件
- vue.js组件之间的通讯-----父亲向儿子传递数据,儿子接收父亲的数据
- vue.js 组件之间传递数据
- vue.js 组件之间传递数据
- Vue.js的组件(二)父组件与子组件的数据联系 之 父传子
- Vue.js的组件(四)父组件与子组件的数据联系 之 双向绑定
- vuejs组件交互 - 01 - 父子组件之间的数据交互
- vue.js---父子组件之间简单的数据传递
- vue.js组件之间传递数据的方法
- Vue.js实战之组件之间的数据传递
- vue.js 组件之间传递数据
- vuejs父子组件之间数据交互详解
- vue.js组件之间通讯的数据双向绑定----父亲把数据传递给儿子,儿子更改数据后,重新发送给父亲,父亲数据更改后,属性会重新发送个儿子,儿子刷新新数据
- vue.js 组件之间传递数据
- vue.js 学习之组件之间数据传递详解
- Vuejs——(9)组件——props数据传递
- vue组件之间的数据通讯小例子
- Vue 爬坑之路(二)—— 组件之间的数据传递
- vue.js之父组件向子组件传递数据
- Vue2.0子同级组件之间数据交互方法