自定义输入框v-modal,2.0写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
</head>
<body>
<div id="app">
<my-input v-model="msg" />
</div>
<script type="text/javascript">
Vue.component('myInput', {
template: '<input :value="value" ref="ipt" @input="change_input_handle" />',
props: {
value: String
},
methods: {
change_input_handle() {
this.$emit('input', this.$refs.ipt.value);
}
}
})
new Vue({
el: "#app",
data() {
return {
msg: '测试'
}
},
watch:{
msg(value){
console.log(value)
}
}
})
</script>
</body>
</html>
自定义v-modal,3.0写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
</head>
<body>
<div id="app">
<my-input v-model="msg" />
</div>
<script type="text/javascript">
Vue.component('myInput', {
template: '<input :value="modelValue" ref="ipt" @input="change_input_handle" />',
props: ['modelValue'],
methods: {
change_input_handle() {
this.$emit('update:modelValue', this.$refs.ipt.value);
}
}
})
new Vue({
el: "#app",
data() {
return {
msg: '测试'
}
},
watch:{
msg(value){
console.log(value)
}
}
})
</script>
</body>
</html>
浙公网安备 33010602011771号