vue ref

vue 通过ref 获取子组件属性

效果

vue

源码

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
47
48
49
50
<html>
<header>
<title>Vue.js ref</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</header>

<body>
<div id="box">
<button @click="handlerClick">Click</button>
<br>
INPUT
<br>
<input type="text" ref="mytext">
<br>
子组件
<child ref="mychild"></child>
</div>
</body>

<script>
Vue.component("child", {
template: `<div>

child 属性
</div>`
,
data() {
return {
myvalue: 123123
}
}
})

var vm = new Vue({
el: "#box",
data: {
},
methods: {
handlerClick() {
console.log("输入的值为:", this.$refs.mytext.value)
console.log("子组件值为:", this.$refs.mychild.myvalue)
}
}
})


</script>

</html>