-
Notifications
You must be signed in to change notification settings - Fork 0
/
32.组件通信5——使用自定义事件的表单输入组件.html
51 lines (47 loc) · 1.2 KB
/
32.组件通信5——使用自定义事件的表单输入组件.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
<!--
自定义事件——> 货币输入表单
PS:子组件有独立作用域,无法使用父组件的属性,因此要在props里定义自己的属性。
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>货币输入表单 </title>
<script type="text/javascript"src="vue.js"></script>
</head>
<body>
<div id="main">
<currency-input ></currency-input>
</div>
</body>
<script type="text/javascript">
Vue.component('currency-input', {
template: '\
<span>\
$\
<input\
ref="input"\
v-bind:value="value"\
v-on:input="updateValue($event.target.value)"\
>\
</span>\
',
props: ['value'],
methods: {
updateValue: function (value) {
var formattedValue = value.trim().slice(0, value.indexOf('.') + 3)
if (formattedValue !== value) {
this.$refs.input.value = formattedValue
}
// Emit the number value through the input event
this.$emit('input', Number(formattedValue))
}
}
})
new Vue({
el: '#main',
data: {
}
})
</script>
</html>