vue refs属性基本使用

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">

</div>
<script src="https://mirrors.idiyrom.com/web/js/vue.mini.js"></script>
<script src="https://mirrors.idiyrom.com/web/js/vue-router.js"></script>
<script>
Vue.component("Test", {
data() {
return {

}
},
template: `
<div class="Test">
我是测试组件1
</div>`
});

Vue.component("Test2", {
data() {
return {

}
},
template: `
<div class="Test2">
我是测试组件2
</div>`
});

let App = {
data() {
return {

}
},
template: `
<div>
<input type="text" ref="aa">
<Test ref="bb"></Test>
<Test2 ref="cc"></Test2>
</div>`,
mounted() {
//获取焦点
//this.$refs.aa 获取原始DOM
this.$refs.aa.focus();
console.log(this.$refs.bb.$parent); //获取父组件
console.log(this.$refs.bb.$root); //获取根组件
console.log(this.$children);

}
};

new Vue({
el: '#app',
data() {
return {

}
},
template: `<App></App>`,
components: {
App
},
});
</script>
</body>
</html>