[plugin:vite:vue] Transform failed with 1 error:
/home/projects/vue3-vite-typescript-starter-jkcbyx/src/App.vue:33:73:
ERROR: Invalid assignment target
"/home/projects/vue3-vite-typescript-starter-jkcbyx/src/App.vue:33:73"
Invalid assignment target
31 | ? (_openBlock(), _createElementBlock("div", _hoisted_2, [
32 | _withDirectives(_createElementVNode("textarea", {
33 | "onUpdate:modelValue": _cache[0] || (_cache[0] = $event => (($setup.np?.description) = $event))
| ^
34 | }, null, 512 /* NEED_PATCH */), [
35 | [
App.vue:<script setup lang="ts">
import { ref } from 'vue'
interface Thing {
description: string
}
const np = ref<Thing>({
description: 'asdf asdf asdf',
})
</script>
<template>
{{ np?.description }}
<br />
<textarea v-model.trim="np?.description"></textarea>
</template>
感谢这里的任何帮助 <3
这个问题比较让人困惑。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
您不能将双重绑定 (
v-model) 与可选链接 (np?.description) 结合使用。双重绑定意味着 getter 和 setter。当 np 为 false 时,您希望设置器设置什么?我知道您将其包装在 v-if 中,但可选链接告诉 v-model 目标对象结构可能是未定义的,这是一个无效的赋值目标。一种方法是创建一个计算的
description,您可以在其中指定当np的当前值时如何设置np.description> 允许:const description = computed({ get() { return np.value?.description || '' }, set(description) { if (typeof np.value?.description === 'string') { np.value = { ...np.value, description } } }, })在此处查看它的工作原理:https: //stackblitz.com/edit/vue3-vite-typescript-starter-wrvbqw?file=src%2FApp.vue
上面是一个非常通用的解决方案(当您实际上确实需要使用
v-model的可选链接时)。在您的情况下,一个更简单的替代方案(可能是因为您将
包装在v-if="np"中),不使用v 的可选链接-model根本:将
v-model.trim="np?.description"替换为v-model.trim="np.description"。它会起作用。