嗨,我想在v模型中使用如下函数(在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.
我在googled上搜索了错误,发现在vue 3中不允许使用v模型中的函数。有谁知道这样做的方法吗?提前谢谢。
发布于 2021-12-01 20:30:37
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://stackoverflow.com/questions/70189977
复制相似问题