/
ref.html
71 lines (63 loc) · 2.02 KB
/
ref.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Use Ref to Get Elements</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<div id="app">
<!-- 1、获取页面 DOM 元素 -->
<input type="text" ref="msgText" v-model="msg" />
<button @click="getElement">获取元素值</button>
<hr>
<!-- 2、获取子组件 -->
<child ref="childComponent"></child>
</div>
<template id="child">
<div>
<input type="datetime" name="datetime" v-model="local">
<button @click="getLocalData">获取当前时间</button>
</div>
</template>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: 'Hello ref'
},
// beforeMount() {
// console.log('beforeMount: ' + this.$refs.msgText.value)
// },
mounted() {
console.log('mounted: ' + this.$refs.msgText.value)
},
methods: {
getElement() {
console.log('input 输入框的值为:' + this.$refs.msgText.value)
this.$refs.childComponent.getLocalData()
console.log('子组件 input 输入框的值为:' + this.$refs.childComponent.local)
}
},
components: {
'child': {
template: '#child',
data() {
return {
local: ''
}
},
methods: {
getLocalData() {
var date = new Date()
this.local = date.toLocaleString()
}
},
}
}
});
</script>
</body>
</html>