嗨,我想在 v-model(在 Vue 3 中)中使用一個函式,如下所示:
<template>
<input v-model="sayHello('Jhon')">
</template>
<script>
export default{
methods:{
sayHello(name){
return "Hello " name
}
}
}
</script>
但代碼回傳此錯誤:
VueCompilerError: v-model value must be a valid JavaScript member expression.
我用谷歌搜索了錯誤,發現你不允許在 vue 3 中使用 v-model 中的函式。有人知道這樣做的方法嗎?提前致謝。
uj5u.com熱心網友回復:
v-model不是用于處理輸入更改的正確指令。如果要在更改時呼叫函式,請使用v-on帶有input事件的指令:
<script setup>
const onChange = e => {
if (e.target.value === 'Jhon') sayHello()
}
const sayHello = () => alert('hello')
</script>
<template>
<h3>Type <code>Jhon</code></h3>
<input @input="onChange" />
</template>
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/371635.html
下一篇:在VUE.JS中傳遞多個引數問題
