vue.js2.0父组件点击触发子组件方法
<body>
<div id="counter-event-example">
<p>{{ total }}</p>
<button-counter v-on:ee="incrementTotal"></button-counter>
<button-counter v-on:ee="incrementTotal"></button-counter>
</div>
<script>
Vue.component('button-counter', {
template: '<button v-on:click="increment">{{ counter }}</button>',
data: function () {
return {
counter: 0
}
},
methods: {
increment: function () {
this.counter += 1
this.$emit('ee', 'cc' )
}
},
})
new Vue({
el: '#counter-event-example',
data: {
total: 'arg'
},
methods: {
incrementTotal: function (b) {
this.total = b + '1';
}
}
})
</script>
</body>
子组件通过$emit触发父组件的事件,$emit后面的参数是向父组件传参,注意,父组件的事件处理函数直接写函数名即可,不要加(),参数直接传递到了父组件的methods的事件处理函数了。
另外,写一个小拾遗。vue子组件用了定义模板组件功能,然后在父组件里定义一个HTML元素绑定这个子组件后才能在父组件通过这个HTML元素使用。
再说一个非常方便的v-ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="vue.js"></script>
</head>
<body>
<div id="parent">
<input type="text" name="" id="" v-model="msg" />
<input type="button" id="" value="dianji" @click="clickDt" />
<user-profile ref="profile"></user-profile>
</div>
<script>
Vue.component('user-profile', {
template: '<span>{{ msg }}</span>',
data: function () {
return {
msg: 123
};
},
methods: {
greet: function (msg) {
console.log(msg);
}
}
})
// var parent = new Vue({el: '#parent'});
// var child = parent.$refs.profile;
// child.greet();
new Vue({
el:"#parent",
data:{
msg:""
},
methods: {
clickDt(){
this.$refs.profile.greet(this.msg);
}
}
})
</script>
</body>
</html>
Vue2.0组件间数据传递
Vue1.0组件间传递
使用$on()监听事件;
使用$emit()在它上面触发事件;
使用$dispatch()派发事件,事件沿着父链冒泡;
使用$broadcast()广播事件,事件向下传导给所有的后代
Vue2.0后$dispatch(),$broadcast()被弃用,见https://cn.vuejs.org/v2/guide/migration.html#dispatch-和-broadcast-替换
1,父组件向子组件传递场景:Father上一个输入框,根据输入传递到Child组件上。
父组件:
<template> <div> <input type="text" v-model="msg"> <br> //将子控件属性inputValue与父组件msg属性绑定 <child :inputValue="msg"></child> </div> </template> <style> </style> <script> export default{ data(){ return { msg: '请输入' } }, components: { child: require('./Child.vue') } } </script
子组件:
<template> <div> <p>{{inputValue}}</p> </div> </template> <style> </style> <script> export default{ props: { inputValue: String } } </script>
2,子组件向父组件传值场景:子组件上输入框,输入值改变后父组件监听到,弹出弹框
父组件:
<template> <div> //message为子控件上绑定的通知;recieveMessage为父组件监听到后的方法 <child2 v-on:message="recieveMessage"></child2> </div> </template> <script> import {Toast} from 'mint-ui' export default{ components: { child2: require('./Child2.vue'), Toast }, methods: { recieveMessage: function (text) { Toast('监听到子组件变化'+text); } } } </script>
子组件:
<template> <div> <input type="text" v-model="msg" @change="onInput"> </div> </template> <script> export default{ data(){ return { msg: '请输入值' } }, methods: { onInput: function () { if (this.msg.trim()) { this.$emit('message', this.msg); } } } } </script>
vue2.0父子组件以及非父子组件如何通信
1.父组件传递数据给子组件
父组件数据如何传递给子组件呢?可以通过props属性来实现
父组件:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
子组件通过props来接收数据:
方式1:
- 1
- 1
方式2 :
- 1
- 2
- 3
- 1
- 2
- 3
方式3:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
这样呢,就实现了父组件向子组件传递数据.
2.子组件与父组件通信
那么,如果子组件想要改变数据呢?这在vue中是不允许的,因为vue只允许单向数据传递,这时候我们可以通过触发事件来通知父组件改变数据,从而达到改变子组件数据的目的.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
父组件:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3.非父子组件通信
如果2个组件不是父子组件那么如何通信呢?这时可以通过eventHub来实现通信.
所谓eventHub就是创建一个事件中心,相当于中转站,可以用它来传递事件和接收事件.
- 1
- 1
组件1触发:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
组件2接收:
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
这样就实现了非父子组件之间的通信了.原理就是把Hub当作一个中转站!
-
顶
-
踩
相关文章
- vue中用解构赋值的方法引入组件
- 【Vue 快速入门系列】github用户头像搜索小案例(VueResource、axios实战练习)
- Vue.js devtools使用方法
- vue中使用refs出现undefined的解决方法
- Vue.js:返回本地时间日期利用双大括号和v-text单项绑定文本两种方法实现
- [Full-stack] 增量开发框架 - Vue.js
- Vue简单了解
- vue-router报错Uncaught (in promise)及解决方法
- vue打包后 点击index.html 找不到静态资源的解决方法
- vue组件重新加载的方法(含选择器重置的方法)
- Vue 父组件调用子组件的方法
- Vue自定义指令使用方法详解 和 使用场景
- Vue系列(二):发送Ajax、JSONP请求、Vue生命周期及实例属性和方法、自定义指令与过渡
- vue全局使用axios的方法
- vue中 表头th 合并单元格,且表格列数不定的动态渲染方法
- 【vue2】vue全家桶介绍,学习vue必备
- 【Vue全家桶】细说组件中的生命周期
- vue非常实用的几行代码【日期处理、字符串处理、数组处理、颜色操作】
- Vue开源UI框架和项目(持续更新中...)
- Vue工程搭建Leaflet项目第一弹:地图展示
- 深入了解 Vue.js 是如何进行「依赖收集]
- 关于Vue.js 使用v-cloak后仍显示变量的解决方法
- vue实例方法
- Vue 进阶教程之:非父子组件通信方法(非Vuex)
- Vue引入远程JS文件
- 详解 Vue & Vuex 实践