const todoId = ref(1)
const data = ref(null)
watch(
todoId,
async () => {
const response = await fetch(
`https://jsonplaceholder.typicode.com/todos/${todoId.value}`
)
data.value = await response.json()
},
{ immediate: true }
)
watchEffect(async () => {
const response = await fetch(
`https://jsonplaceholder.typicode.com/todos/${todoId.value}`
)
data.value = await response.json()
})
<!-- 在 "change" 事件后同步更新而不是 "input" -->
<input v-model.lazy="msg" />
如果你想让用户输入自动转换为数字,你可以在 v-model 后添加 .number 修饰符来管理输入:
<input v-model.number="age" />
<input v-model.trim="msg" />
<MyComponent v-for="item in items" :key="item.id" />
<MyComponent
v-for="(item, index) in items"
:item="item"
:index="index"
:key="item.id"
/>