最近在学习Vue,官方文档上有些内容没有给出示例,于是就自己整理练习。
原文链接
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>在自定义组件中创建 v-model 功能</title>
<script src="./js/vue.global.js"></script>
</head>
<body>
<div id="app">
<custom-input v-model="searchText"></custom-input>
<h3> 自定义组件的v-model:{{ searchText }} </h3>
</div>
<script>
const app = Vue.createApp({
data() {
return {
searchText: ''
}
}
})
app.component('custom-input',{
props: ['modelValue'],
emits: ['update:modelValue'],
template:`
<input
:value="modelValue"
@input="$emit('update:modelValue',$event.target.value)" />
`
})
app.mount('#app');
</script>
</body>
</html>