【发布时间】:2022-01-16 08:45:31
【问题描述】:
我正在尝试使用带有 Typescript 的 Vue 3 Composition API 构建一个基本的待办事项列表应用程序。我将表单中的提交函数配置为在设置函数中调用addTodo()。我的意图是将用户输入的值推送到listItems 数组,该数组使用ref 方法初始化。在我的addTodo() 函数中,我添加了listItems.value.push(newTodo.value) 以便将输入的值传递给数组。但是,我在 newTodo.value 参数上遇到错误,说 Argument of type 'string' is not assignable to parameter of type 'never'。我是 Vue 3 中的 Composition API 的新手。我该如何解决这种类型的错误?
请参阅下面的代码:
模板
<template>
<div class="container">
<form @submit.prevent="addTodo()">
<label>New ToDo</label>
<input
v-model="newTodo"
name="newTodo"
autocomplete="off"
>
<button>Add ToDo</button>
</form>
<div class="content">
<ul>
<li v-for="listItem in listItems" :key="listItem">
<h3>{{ listItem }}</h3>
</li>
</ul>
</div>
</div>
</template>
脚本
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
name: 'Form',
setup() {
const newTodo = ref('');
const listItems = ref([]);
const addTodo = () => {
listItems.value.push(newTodo.value)
newTodo.value = ''
}
return { newTodo, listItems, addTodo }
}
});
</script>
【问题讨论】:
标签: typescript vue.js vue-component vuejs3 vue-composition-api