我正在创建一个组件,该组件接受一组数据(使用基于接口的类型化props ),然后传入第二个props ,该props 动态地 Select 我想要在数据对象中引用的属性.
我将Vue 3脚本设置语法与TypeScrip和fineProps一起使用.
<script setup lang="ts">
interface DataRow {
'id': number
'height': number
'strideLength': number
'distanceTravelled': number
'timestamp': Date
}
const props = defineProps<{
data: DataRow[]
xAxis: string
yAxis: string
}>
</script>
<template>
X Axis: {{ props.data[xAxis] }}
Y Axis: {{ props.data[YAxis] }}
<pre>
{{ data }}
</pre>
</template>
我正在try 传递一个接口属性,这样我就可以动态地 Select 要在绘图的x/y轴上显示的内容.目前,我正在使用一个字符串,但由于它是非类型化的,我收到了Linter错误.
const props = defineProps<{
data: DataRow[]
xAxis: string <- this should only be properties of my DataRow interface
yAxis: string <- this should only be properties of my DataRow interface
}>
<my-component
:data="[{id: 1, height: 2, strideLength: 0.5, distanceTravelled: 5, timestamp: new Date()}]"
:xAxis="timestamp"
:yAxis="height" />
有没有更好的方法来做这件事?