As I implemented the code in the Vue 3 setup block to retrieve the input value according to this answer, here is a snippet of the code:
import { defineComponent } from "vue";
import { defineProps, defineEmits } from 'vue'
export default defineComponent({
setup() {
const props = defineProps({
modelValue: String
})
const emit = defineEmits(['update:modelValue'])
function updateValue(value: any) {
emit('update:modelValue', value)
}
}
However, upon running the app, I encountered an error:
option.js:17388 Uncaught TypeError: emit is not a function
at Proxy.updateValue (option.js:17388:13)
at onInput._cache.<computed>._cache.<computed> (option.js:17428:78)
at callWithErrorHandling (option.js:7359:22)
at callWithAsyncErrorHandling (option.js:7368:21)
at HTMLInputElement.invoker (option.js:15384:90)
Even though I have defined emit, why does it still show that emit is not a function? Here is my complete code for the Vue 3 component:
<template>
<div id="app">
<div id="wrap">
<label>
{{ username }}
</label>
<ul class="nav nav-tabs">
<li>
<input
:value="props"
placeholder="username"
v-on:input="updateValue($event.target.value)"/>
<input v-model="password" placeholder="password" />
<button @click="login">Login</button>
</li>
</ul>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { defineProps, defineEmits } from 'vue'
export default defineComponent({
setup() {
const props = defineProps({
modelValue: String
})
const emit = defineEmits(['update:modelValue'])
function updateValue(value: any) {
emit('update:modelValue', value)
}
const login = () => {
debugger
alert(props.modelValue);
};
debugger
return {
login,
updateValue,
props
};
},
components: {},
});
</script>
<style lang="scss" scoped>
</style>
I aim to capture the user's input for the username from the template input. It seems that the current method is not effective. How can I resolve this issue? I have attempted updating the @vue/compiler-sfc
to version 3.2.31, but the problem persists.