Vue 3框架编写的应用程序中数组的用法
Le Sun 23 February 2025
姑娘说,要有数组。
import { ref } from 'vue'
const hers_fancy_integers = ref([1, 3, 5, 7, 9, 2, 4, 6, 8, 10])
使用数组:
hers_fancy_integers.value
姑娘发现,使用ref之物,读取它的值需要借助value。
如若直接使用数组,可能会有惊吓。例如:
const her_scares_it = ref([""])
姑娘想要得知her_scares_it中第一个元素的值,用her_scares_it后,得到""。姑娘想要的是空白字符,但却得到了鸳鸯引号。
"新年都未有芳华,二月初惊见草芽。" 姑娘说,要添置些新物。
let novel = 0x01
hers_fancy_integers.value.push(novel)
姑娘想要移除某些元素。
const her_hated_index = 3
hers_fancy_integers.value.splice(her_hated_index, 1)
“一尺深红胜曲尘,天生旧物不如新。”姑娘想要替换数组中的某些元素。
const old_dust_index = 2
const new_landscape_value = 100
hers_fancy_integers.value[old_dust_index] = new_landscape_value
“新语见称应有意,当时人未说诗书。”姑娘发现:
用了vue的ref后,对响应数组的值的修改就会变成对ref之物的value的修改,而不是对数组的修改。