最近总是遇到稀奇古怪的问题,明明定义的对象有属性值,还是报undefined
let states = reactive({
inputVisible: false,
inputValue: '',
tags:[]
});
// 点击添加标签的方法
const showInput = () => {
states.inputVisible = true;
nextTick(() => {
inputRef.value.focus();
})
};
// tag标签失去焦点之后添加的tags
const handleInputConfirm = () => {
console.log(states);
}
打印出来的states中的tags竟然是undefined,真的不能理解。最难受的是,我别的组件也有这样写的地方,但是没有问题
大家有遇到这样的情况吗?咋解决的呀
哎嗨,周末静下心来再看的时候才发现,这个属性再别的事件触发的时候,被改变了,真的太大意了
你没对 tags 进行赋值 。按道理 打印 应该是个空数组
最好能提供一个复现问题的完整示例和vue
的版本信息。
这里简单模拟了一下,并不存在你描述的问题,vue
版本是3.2.37
<script setup lang="ts">
import { reactive } from 'vue'
const state = reactive({
inputVisible: false,
inputValue: '',
tags:[]
})
console.log(state)
</script>