使用ts定义,可限制参数的类型
新建ts文件
// 定义接口限制对象属性
export interface personInter {
name:string,
age:number,
gender:string
}
// 使用泛型
// export type personArr = Array<personInter>
// 另一种写法
export type personArr = personInter[]
引入并使用
<template>
<div class="person">
???
</div>
</template>
<script lang="ts" setup name="Person">
import {type personInter,type personArr} from '@/types'
let person1:personInter = {name:'111',age:20,gender:'F'}
let person2:Array<personInter> = [
{name:'111',age:20,gender:'F'},
{name:'222',age:20,gender:'F'},
{name:'333',age:20,gender:'F'}
]
let person3:personArr = [
{name:'111',age:20,gender:'F'},
{name:'222',age:20,gender:'F'},
{name:'333',age:20,gender:'F'}
]
</script>
<style scoped>
.person {
background-color: skyblue;
box-shadow: 0 0 10px;
border-radius: 10px;
padding: 20px;
}
button {
margin : 0 5px;
}
li {
font-size : 20px;
}
</style>
标签:vue,20,name,自定义,personInter,gender,age,TS,type
From: https://www.cnblogs.com/ayubene/p/18081266