withDefaults使用
父组件
<script setup lang="ts">
import RefPractice from '@/components/ref_practice.vue'
import { type PersonIner } from '@/components/type'
import {ref, reactive} from 'vue'
let personList = reactive<PersonIner[]>([
{id: 1, name: 'xxx', age: 18},
{id: 2, name: 'xxx2', age: 19}
])
</script>
<template>
<RefPractice :list="personList" @click="chackRef"></RefPractice>
</template>
子组件
<script setup lang="ts">
// withDefaults会飘红波浪线,This is only usable inside <script setup>, is compiled away in the output and should not be actually called at runtime.
// 可以正常运行的
// 有哪位好心人知道为啥吗
import { withDefaults,ref } from 'vue'
import { type Persons } from '@/components/type'
// 第一种写法(箭头函数写 {花括号} 记得加 return)
withDefaults(defineProps<{list?:Persons}>(),{
list: () => {
return [{id: 3333, name: '毛利小五郎', age: 90}]
}
})
// 第二种写法
// withDefaults(defineProps<{list?:Persons}>(),{
// list: () => [{id: 3333, name: '毛利小五郎', age: 90}]
// })
</script>
<template>
<div class="refPractice">
<p v-for="item in list" :key="item.id">{{item.name}} - {{item.age}}</p>
</div>
</template>
</style>