vue开发:vue父子组件与非父子组件之间的通信
来源:互联网 发布:nginx跳转保持url不变 编辑:程序博客网 时间:2024/06/06 03:59
之前关于vue.js2.0父组件的一点学习,最近需要回顾,就顺便发到随笔上了
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<
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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!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组件上。
父组件:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<template>
<div>
<input type=
"text"
v-model=
"msg"
>
<br>
<child :inputValue=
"msg"
></child>
</div>
</template>
<style>
</style>
<script>
export
default
{
data(){
return
{
msg:
'请输入'
}
},
components: {
child: require(
'./Child.vue'
)
}
}
</script>
子组件:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<template>
<div>
<p>{{inputValue}}</p>
</div>
</template>
<style>
</style>
<script>
export
default
{
props: {
inputValue: String
}
}
</script>
2,子组件向父组件传值场景:子组件上输入框,输入值改变后父组件监听到,弹出弹框
父组件:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
<div>
<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>
子组件:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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
<parent>
<child :child-msg=
"msg"
></child>
</parent>
data(){
return
{
msg: [1,2,3]
};
}
子组件通过props来接收数据:
方式1:
方式2 :
?
1
2
3
props: {
childMsg: Array
}
方式3:
?
1
2
3
4
5
6
props: {
childMsg: {
type: Array,
default
: [0,0,0]
}
}
这样呢,就实现了父组件向子组件传递数据.
2.子组件与父组件通信
那么,如果子组件想要改变数据呢?这在vue中是不允许的,因为vue只允许单向数据传递,这时候我们可以通过触发事件来通知父组件改变数据,从而达到改变子组件数据的目的.
子组件:
?
1
2
3
4
5
6
7
8
9
<template>
<div @click=
"up"
></div>
</template>
methods: {
up() {
this
.$emit(
'upup'
,
'hehe'
);
}
}
父组件:
?
1
2
3
4
5
6
7
8
<div>
<child @upup=
"change"
:msg=
"msg"
></child>
</div>
methods: {
change(msg) {
this
.msg = msg;
}
}
3.非父子组件通信
如果2个组件不是父子组件那么如何通信呢?这时可以通过eventHub来实现通信.
所谓eventHub就是创建一个事件中心,相当于中转站,可以用它来传递事件和接收事件.
组件1触发:
?
1
2
3
4
5
6
<div @click=
"eve"
></div>
methods: {
eve() {
Hub.$emit(
'change'
,
'hehe'
);
}
}
组件2接收:
?
1
2
3
4
5
6
<div></div>
created() {
Hub.$on(
'change'
, () => {
this
.msg =
'hehe'
;
});
}
这样就实现了非父子组件之间的通信了.原理就是把Hub当作一个中转站!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。