App.vue是项目的一个入口 引入之后,要注册才能使用。
父向子组件传参: 父组件直接定义属性(msg)和属性值(something interesting)-子组件想要取得父组件中定义的属性值(something interesting)-在子组件中定义props,属性名(msg)在数组中-子组件即可得到该参数(this.msg)。
子向父组件传参: 在componentA.js中
<button v-on:click='onClickMe'>open mouse</button>子组件中给按钮绑定一个click事件,执行onClickMe函数,
onClickMe:function(){ this.$emit('listen-from-child',this.msg); console.log(this.msg); }在onClickMe函数中触发自定义的listen-from-child事件,在父组件中app.vue文件中
<componentA msgfromfather='you die' v-on:listen-from-child='listenChild'></componentA>componentA中绑定了listen-from-child事件,click之后触发到该事件,又执行app.vue中的listenChild函数,
listenChild:function(msg){ this.childSay=msg; }让this.childSay的值为子组件中传入的this.msg。想要在页面中显示this.childSay的值,还要在app.vue中的data中声明childSay属性。
<p>childsay:{{childSay}}</p> data:function(){ return { title: '<span>?</span>qmm to do a list', items:Store.fetch(), liClass:'thisisliclass', newItem:'', childSay:''//在这儿声明 } }这样在父组件里就能接收到子组件中传的参啦。 dispatch, broadcast在vue2.0中已经被弃用。
