我正在使用 Vue.js 3 和 Typescript 开发单页应用程序。
该问题影响视图和单个文件组件。People.vue从后端检索数据并PersonRow.vue使用v-for. 尽管明确定义了数据属性类型,但我在浏览器控制台中收到警告:
[Vue warn]: Invalid prop: Type check failed for prop "person". Expected Person, got Object
一切正常,我可以将属性类型更改PersonRow.vue为Object以消除警告,但我希望类型检查正常工作。
People.vue
<template>
<div class="container">
<PersonRow v-for="person in people" :key="person.id" :person="person" />
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { Person, getPeople } from '../services/person'
import PersonRow from '@/components/PersonRow.vue'
export default defineComponent({
components: {
PersonRow
},
data () {
return {
people: new Array<Person>() …Run Code Online (Sandbox Code Playgroud) 我需要一个不可变列表,我可以从中派生出第二个不可变列表,该列表保留前一个列表的所有元素以及 Java 中的一个附加元素(无需附加库)。
注意:这个问题类似于将单个元素添加到不可变集合的有效且优雅的方法是什么?但我需要一个清单,而且没有番石榴。
到目前为止我尝试过的:
var list = List.of(someArrayOfInitialElements);
var newList = Stream.concat(list.stream(), Stream.of(elementToAppend))
.collect(CollectorsCollectors.toUnmodifiableList());
Run Code Online (Sandbox Code Playgroud)
这会起作用,但创建一个流并一个一个地复制元素对我来说似乎效率低下。鉴于List.of()将数据存储在基于字段或基于数组的数据结构中,您基本上可以批量复制内存。
有没有比使用流更有效的解决方案?我缺少的 Java 标准库中更好的数据结构?