尚矽谷影片連結
使用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>