v-model
# 当你在元素上绑定 v-model的时候
- vue相当于给你做了两件事
<template>
<div>
<input v-model="value" type="text"> // 当你在元素上绑定 v-mode的时候 相当于下面这个
<input type="text" :value="value" @input="value = $event.target.value" />
{{ value }}
</div>
</template>
<script>
import one from "./views/one.vue";
export default {
components: { one },
data() {
return {
value: "",
};
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 当你在组件上绑定 v-mode的时候
- vue相当于给你做了两件事
父组件
<template>
<div>
<one v-model="value"></one> // 当你在组件上绑定 v-mode的时候 相当于下面这个
<one :modelvalue="value" @update:modelValue="value=$event.target.value"></one>
{{ value }}
</div>
</template>
<script>
import one from "./views/one.vue";
export default {
components: { one, loding, error },
data() {
return {
value: "3",
};
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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" @input="add($event)" />
</div>
</template>
<script>
export default {
emits: ["update:modelValue"],
data() {
return {};
},
props: {
modelValue: String,
},
methods: {
add(e) {
this.$emit("update:modelValue", e.target.value);
},
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 使用计算属性实现
父组件
<template>
<div>
<one v-model="value"></one> // 当你在组件上绑定 v-mode的时候 相当于下面这个
<one :modelvalue="value" @update:modelValue="value=$event.target.value"></one>
{{ value }}
</div>
</template>
<script>
import one from "./views/one.vue";
export default {
components: { one, loding, error },
data() {
return {
value: "3",
};
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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="value" />
</div>
</template>
<script>
export default {
emits: ["update:modelValue"],
data() {
return {};
},
props: {
modelValue: String,
},
computed: {
value: {
set(v) {
this.$emit("update:modelValue", v);
},
get(v) {
return this.modelValue;
},
},
},
};
</script>
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
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
# 当一个组件有多个v-model
- 当子组件的value发生变化时会赋值给父组件 父组件
<template>
<div>
<one v-model="value" v-model:xx="xx"></one>
</div>
</template>
<script>
import one from "./views/one.vue";
export default {
components: { one, loding, error },
data() {
return {
value: "3",
xx: "4",
};
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
子组件
<template>
<div>
<input type="text" v-model="value" />
<input type="text" v-model="xx" />
</div>
</template>
<script>
export default {
emits: ["update:modelValue", "update:xx"],
data() {
return {};
},
props: {
modelValue: String,
xx: String,
},
computed: {
value: {
set(v) {
this.$emit("update:modelValue", v);
},
get(v) {
return this.modelValue;
},
},
xx: {
set(v) {
this.$emit("update:xx", v);
},
get(v) {
return this.xx
},
},
},
};
</script>
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
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